Can't set sensor value

I try to implement a data fetching protocol to my system. The thing is I can succesfully retrieve data, I can see it in the Mycodo logs. But when I try to set this data to return_dict it just doesn’t. I don’t understand why, if someone can help me out I’d really appreciate it. And if there is anything I’m missing please let me know. Thanks

Here is the get_measurement function:

def get_measurement(self):
        self.logger.info(f"Is enabled: {self.is_enabled(0)}")
        self.return_dict = copy.deepcopy(measurements_dict)
        try:
            response = self.read_do(address)
        
            if response:
                response_dict = json.loads(response)
                self.logger.info(f"Received response as dict: {response_dict} at {time.strftime('%H:%M:%S')}")
                if match_address(response_dict,address):
                    try:
                        response_value = extract_value_if_sensor_exists(response_dict)
                        self.logger.info(f"Setting value: {response_value}")
                        self.value_set(0, float(response_value))
                        return self.return_dict
                    except ValueError:
                        self.logger.debug("Failed to convert the response to float.")
                        return None
                else:
                    self.logger.info(f"Response dict: {response_dict}, address: {address}")
                    self.logger.debug("Response doesn't match with the sensor.")
                    self.logger.info("Response doesn't match with the sensor.")
                    return None
            else:
                self.logger.debug("No valid response received.")
                return None
            
        except Exception as msg:
            self.logger.exception(f"Input read failure: {msg}")
            return None
    

And here is the Mycodo logs:

2024-11-07 15:54:29,082 - INFO - mycodo.inputs.test_slave_3fb80327 - Command to send: sla1_do_r
2024-11-07 15:54:29,082 - INFO - mycodo.inputs.test_slave_3fb80327 - Command returned: {"sla1":[{"do":0.946}]}
2024-11-07 15:54:29,082 - INFO - mycodo.inputs.test_slave_3fb80327 - Received response as dict: {'sla1': [{'do': 0.946}]} at 15:54:29
2024-11-07 15:54:29,082 - INFO - mycodo.inputs.test_slave_3fb80327 - Setting value: 0.946
2024-11-07 15:54:43,823 - INFO - mycodo.inputs.test_slave_3fb80327 - Is enabled: True
2024-11-07 15:54:44,076 - INFO - mycodo.inputs.test_slave_3fb80327 - Command to send: sla1_do_r
2024-11-07 15:54:44,076 - INFO - mycodo.inputs.test_slave_3fb80327 - Command returned: {"sla1":[{"do":0.051}]}
2024-11-07 15:54:44,076 - INFO - mycodo.inputs.test_slave_3fb80327 - Received response as dict: {'sla1': [{'do': 0.051}]} at 15:54:44
2024-11-07 15:54:44,077 - INFO - mycodo.inputs.test_slave_3fb80327 - Setting value: 0.051

1 - What sensor are you trying to use? Is it a sensor that is supported by Mycodo?
2 - It seems like you are trying to do something that the Mycodo database already does… why?
3 - What exactly are you attempting to accomplish? Are you trying to create a Conditional Controller? Are you trying to create a new Input Module for the sensor you are using?

1- It’s not in Mycodo, this is a custom input that receives data from Arduino.
2- I don’t understand. With self.value_set(0, float(response_value)) I aim to save this value to the Mycodo.
3- I am trying to save response_value to the Mycodo. I’ve tried saving something like self.value_set(0, float(15)) to save 15 number to sensor. But it still didn’t save any value.
Here are __init__ and initialize_input functions if they are going to help something.

    def __init__(self, input_dev, testing=False):
        """Initialize the input."""
        super().__init__(input_dev, testing=testing, name=__name__)
        if not testing:
            self.initialize_input()
            

    def initialize_input(self):
        """Initialize the input module."""
        self.logger.debug("Initialization of Input completed.")

You are still not being clear at all…
You have a sensor attached to an Arduino…
and you are trying to get the measurement from that sensor and send it to a custom Input you are writing for Mycodo???
Is this code you are writing running on the Arduino or on the Raspberry Pi running Mycodo???
How are you sending the measurement data from the Arduino to Mycodo? Wired connection? WiFi connection?

Seems like you are trying to re-invent the wheel.

Instead of using an Arduino, maybe try using a $5 ESP32 microcontroller and running Tasmota on it so you can use the MQTT protocol to send the data to Mycodo over your WiFi network.
Mycodo already has an MQTT Input module to receive data from remote sensors… so why re-invent the wheel?

Thanks for suggestions. Anyway I solved the problem. I figured that since self.value_set command saves values to the database, problem had to do something with database. After changing database to Influxdb 2.x in the settings, it worked. Now I am able to write data and view them.

1 Like