I have a project to read the tank value from a distance using the lora network with meshtastic. I have already connected one meshtastic node to home assisant and I receive the tank level in the published MQTT payload. I try to use the number provided in the MQTT paylaod to set an input number but it is not working. I get an error message saying that it is not a correct value. I have tested the template in developement tools and it reports the number value but my automatisation is not working
Plz format what u post correctly, makes reading it and helping alot easier.
Having said that, states are always a string i believe. So you need to define what u want it to be.
In this case int or float, do not forget to set a default value.
Sorry for the bad code posting. I have edited my initial post and correct it.
I have tried with int(0) but with the same result. The payload output is a text (at least I suppose) and not a number so I cannot use it for calculation.
The sensor is located 850m away from my home. It is working on a small battery and to avoid battery drainage I send the data only every 30 minutes (maybe one hour later). The code I have posted is just a test to see if I can receive the payload as a number. The data received is the distance between the top of my tank and the water. I have 3 tanks interconnected so I need to make small calculation to have the water quantity and it is why I need a number.
The payload is part of a MQTT topic in JSON. I have to create a sensor in home assistant to use it. I have created a MQTT.yaml file and this is the code I use:
Because it is a long RF connection, I have sometimes transmision error and the payload becomes unavailable. This is the reason I want to save it as number in a separate entity
This should keep the last value if the payload is unavialable.
sensor:
- name: "Node 1 payload"
unique_id: "node_1_payload"
state_topic: "msh//2/json/VLongSlow/!e0f5e184"
unit_of_measurement: m # or whatever it is measured in
value_template: >
{% if value_json.payload is defined %}
{{ value_json.payload }}
{% else %}
{{ this.state }}
{% endif %}
Thanks you. I understand that this will have the last good value but what about the calculation?
I need to do (1000 - sensor.node_1_payload) * 3 to have the quantity in liter. The payload data is a string. How can I do?
When it publishes the data to msh//2/json/VLongSlow/!e0f5e184, does it publish it as a retained message? (Meaning the published payload is stored/retained on the broker.)
Thank you Tron. It is working but can you explain me why this:
{{ (1000 - value_json.payload|float(0)) * 3 }}
works in template but not in an automatisation as input number. I receive a message saying that json_value is undefined
@Taras: The value is retained on the broker until it receive a new value but for a reason I don’t understand sometimes the value comes unknow in HA. This was the reason to save it as input number
Check the Log for errors.If the received payload is corrupted or simply not in JSON format, then your template will fail (because it expects JSON for value_json.payload) and so the sensor’s value will be unknown.
value_json is a special variable that contains the incoming data as an object if the incoming data is a JSON string. See here:
So if your MQTT payload is a JSON object, you can use value_json in that sensor definition to extract data from that object. In your case, you are getting the value stored against the payload key.
In your automation, you do not have any incoming data. To set the input number, you need to have the value already stored in a sensor and use that.
Euh…OK…My brain really don’t understand the template logic.
I switch off my meshtastic device yesterday evenig and right now if I look
{{ states('sensor.node_1_payload') | int(0) }}
I have the last value from yesterday. So for me it is stored but I cannot use it in imput number value but the template is working in the developement tool.
I have also modified the sensor in mqtt.yaml for this:
sensor:
- name: "Node 1 payload"
unique_id: "node_1_payload"
state_topic: "msh//2/json/VLongSlow/!e0f5e184"
unit_of_measurement: L
value_template: >
{% if value_json.payload is defined and value_json.payload > 16 and value_json.payload < 150 %}
{{ (1000 - (value_json.payload|float(0) * 10)) *3 }}
{% else %}
{{ this.state }}
{% endif %}
It is fully working as I want but if I test it in the developement tool I get some error message.
My conclusion because I don’t understand how it is working is: somtimes it works, somtimes not. Depending if you have luck or not. Really anoying for me
I didn’t change the code of my automatisation so far. Still the same as in the post 1.
I have a sensor called sensor.node_1_payload. There must be data stored because even when my device is disconnected I can read the last value.
This sensor has a json value I can read by using value_json.payload. Right now because my meshtastic is disconnected, there is no value in the mqtt topic but I have still the last known value in sensor.node_1_payload
So why can I not use the sensor value to set an input number?