MQTT payload as input number

Hello

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

service: input_number.set_value
target:
  entity_id: input_number.niveau_eau_cuves_verger
data: "{{ states('sensor.node_1_payload') | int(0) }}"

I suppose that the payload value is a text and not a number so I cannot use it. How can I convert it?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

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.

{{ states('sensor.node_1_payloadl') | int(0)}}

Why?

If sensor.node_1_payload contains the water level, why does that value have to be duplicated in input_number.niveau_eau_cuves_verger?

In addition, why duplicate the value every 30 minutes and not immediately when the value changes?

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:

sensor:
  - name: "Node 1 payload"
    unique_id: "node_1_payload"
    state_topic: "msh//2/json/VLongSlow/!e0f5e184"
    value_template: "{{ value_json.payload }}" 

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?

UI, Settings, Helpers, Template, Template sensor.

State template of:

{{ (1000 - states('sensor.node_1_payload')|float(0)) * 3 }}

Or do it directly in the sensor config above:

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 %}
        {{ (1000 - value_json.payload|float(0)) * 3 }}
      {% else %}
        {{ this.state }}
      {% endif %}

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

There is no incoming data in the template editor, so value_json is not defined.

Show your latest code.

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?

What does

{{ states('sensor.node_1_payload') }}

return in the template editor?

What are the settings of the input number: min, max and step?

{{ states('sensor.node_1_payload') }}

Report the last value received. Right now 30
The input number minimum is 0
The maximum 100 with a step of 1

Ah, found it.

service: input_number.set_value
target:
  entity_id: input_number.niveau_eau_cuves_verger
data:
  value: "{{ states('sensor.node_1_payload') | int(0) }}"

Ok, so any idea why the automatisation is not working. I receive an error:
Error rendering data template: Result is not a Dictionary

Have you changed your automation to match my code, with value: under data:?