Hi everyone, I need to get sensor value from modbus, and then perform some math actions with it. For example, here my configuration.yaml
modbus:
- name: modbus_hub
type: serial
port: /dev/ttyUSB0
baudrate: 38400
bytesize: 8
method: rtu
parity: N
stopbits: 1
sensors:
- name: Sensor1
unit_of_measurement: °C
address: 40001
slave: 1
input_type: holding
data_type: int16
scan_interval: 1
How can I substract from it value, or use it in another scripts?
Thanks!
tom_l
2
To subtract a number from your sensor value use the scale
option. e.g. to subtract 42:
modbus:
- name: modbus_hub
type: serial
port: /dev/ttyUSB0
baudrate: 38400
bytesize: 8
method: rtu
parity: N
stopbits: 1
sensors:
- name: Sensor1
unit_of_measurement: °C
address: 40001
slave: 1
input_type: holding
data_type: int16
scan_interval: 1
offset: -42
There is also a scale
option.
output = scale * value + offset
Ok, thanks. But how I can perform div/mod operation?
Also, important question is how I can get value of this sensor to use in other scripts?
tom_l
4
Divide by 2 would be:
scale : 0.5
Modular arithmetic function is not available you would have to feed your modbus sensor to a template sensor. E.g. mod 42:
template:
- sensor:
- name: My modified sensor value
state: "{{ states('sensor.sensor1')|float(0) % 42 }}"
unit_of_measurement: "°C"
device_class: temperature
state_class: measurement
You use templates in services in scripts to get the value of your sensor. e.g.
sequence:
- service: input_number.set_value
target:
- entity_id: input_number.answer
data:
value: "{{ states('sensor.sensor1') }}"
1 Like