Modbus value if zero, present last know value - possible?

Hi,
I would like to read some values from modbus device, but if there is no value or some communication disturbing device is sending “0”, and then my scope looks like:


How to make to skip read and use last know value if answer from device = “0” ?

    sensors:
      - name: "AC 1"
        slave: 1
        address: 479
        input_type: holding
        scan_interval: 60
        data_type: uint16
        unit_of_measurement: V
        scale: 0.01
        precision: 1
      - name: "AC 2"
        slave: 1
        address: 480
        input_type: holding
        scan_interval: 60
        data_type: uint16
        unit_of_measurement: V
        scale: 0.01
        precision: 1
      - name: "AC 3"
        slave: 1
        address: 481
        input_type: holding
        scan_interval: 60
        data_type: uint16
        unit_of_measurement: V
        scale: 0.01
        precision: 1

Sensor template

The same was my first idea but how to use previous non zero value when real is equal “0” i have no idea, any examples ?

template:
  - sensor:
    - name: "ac 2 filtered"
      state: >-
        {% if states('sensor.ac_2') == 0 %}
          {{ states('sensor.ac_2_filtered') }}
        {% else %}
          {{ states('sensor.ac_2') }}
        {% endif %}
1 Like

Thanks, the easiest is the simples I don’t know why but I looked to use previous value of modbus read instead of new “filtered”, sometimes black wall in front of eyes and can’t find simple way, once again thank you.

O can change modbus entity to “ac_2_raw”

And keep using old in template “ac_2”

I think it’s not possible to change modbus sensor itself

Hmm I did it exactly like on your example and it is presenting"0" :

    - name: "AC 1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') == 0 %}
          {{ states('sensor.ac_1') }}
        {% else %}
          {{ states('sensor.ac_modbus_1') }}
        {% endif %}
    - name: "AC 1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') > 0 %}
          {{ states('sensor.ac_modbus_1') }}
        {% else %}
          {{ states('sensor.ac_1') }}
        {% endif %}

It is not working…
For test I did:

    - name: "AC_test1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') > 0 %}
          on
        {% else %}
          off
        {% endif %}
    - name: "AC_test2"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') == 0 %}
          off
        {% else %}
          on
        {% endif %}
    - name: "AC_test3"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') != 0 %}
          on
        {% else %}
          off
        {% endif %}

image
So == and != it is working but > it is not working
So now I have the same way of calculation for test (on / off) and other where value is presented, and on test value is always the same (no changes) and on real it is like:

Maybe its need |float
Just as example

This works for me

    - name: "EB3 Import"
      state: >-
        {% set x = states('sensor.eb3_0x0016').split(',')[0] | float %}
        {% if x > 0 %}
          {{ (x / 1000) | round(1) }}
        {% else %}
          {{ states('sensor.eb3_import') }}
        {% endif %}
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing

Just as example, not modbus.

  - platform: mqtt
    name: "EB3 Voltage L1"
    unique_id: EB3_VolL1
    state_topic: "tele/edpbox3/SENSOR"
    value_template: >-
        {% set x = value_json.EB3.VolL1|float %}
        {% if x > 0 %}
          {{ x }}
        {% endif %}
    unit_of_measurement: "V"
    device_class: voltage
    state_class: measurement

It is really surprising me because this is working:

    - name: "AC_test1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_1') | float> 0 %}
          on
        {% else %}
          off
        {% endif %}

This is not working:

    - name: "AC 1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% set tmp = states('sensor.ac_modbus_1') | float %}
        {% if tmp > 0 %}
          {{ states('sensor.ac_1') }}
        {% else %}
          {{ states('sensor.ac_modbus_1') }}
        {% endif %}

this also not working

    - name: "AC 2"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_2') | float > 0 %}
          {{ states('sensor.ac_2') }}
        {% else %}
          {{ states('sensor.ac_modbus_2') }}
        {% endif %}

also

    - name: "AC 2"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% if states('sensor.ac_modbus_2') | float > 0 | float %}
          {{ states('sensor.ac_2') }}
        {% else %}
          {{ states('sensor.ac_modbus_2') }}
        {% endif %}

EDIT:

I find a way !

    - name: "AC 1"
      icon: "mdi:flash"
      unit_of_measurement: "V"
      state: >-
        {% set tmp = states('sensor.ac_modbus_1') | float %}
        {% set tmp2 = states('sensor.ac_1') | float %}
        {% endif %}
        {% if tmp > 0 %}
          {{ tmp }}
        {% else %}
          {{ tmp2 }}
        {% endif %}
1 Like