dar3k
March 23, 2022, 3:23pm
1
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
dar3k
March 23, 2022, 4:42pm
3
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 ?
nikito7
(nikito7)
March 23, 2022, 9:42pm
4
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
dar3k
March 24, 2022, 7:15am
5
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.
nikito7
(nikito7)
March 24, 2022, 4:23pm
6
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
dar3k
March 24, 2022, 7:07pm
7
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 %}
nikito7
(nikito7)
March 25, 2022, 3:35am
8
- 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 %}
dar3k
March 25, 2022, 9:14am
9
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 %}
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:
nikito7
(nikito7)
March 25, 2022, 4:08pm
10
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
nikito7
(nikito7)
March 25, 2022, 4:14pm
11
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
dar3k
March 25, 2022, 4:49pm
12
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