MateoMX
(Mateo Mx)
August 15, 2024, 6:20pm
1
I am using an ESP to control an air compressor and I would like to build in some safeguards so that if my modbus conenction to either a 4-20ma pressor sensor or the connection to compressor’s controller itself becomes disconnected I can take so actions to put the compressor into a safe state (most likely trigger an emergency stop and send me a notification).
Any ideas on how this could be achieved?
I tried adding a lamda to the modbus_sensor but it seems like this does not get execute if the modbus device does not respond to a value.
Does anyone know how I could fire a script when a device either timesout, marked offline or returns a CRC error?
MateoMX
(Mateo Mx)
August 18, 2024, 3:46am
2
Sorry to bump this… Are there any modbus experts out there?
Karosm
(Karosm)
August 18, 2024, 8:31am
3
if (isnan(id(your_comressor_sensor).state)) {id(your_relay).turn_off();}
MateoMX
(Mateo Mx)
August 18, 2024, 7:22pm
4
Thanks @Karosm
Where would I add this to my config? An interval running every second?
I am not sure if this approach would work though, because when the modbus communication fails the entity state does not get set to nan/unknown/unavailable instead, it retains the most recent value successfully read.
Karosm
(Karosm)
August 18, 2024, 7:36pm
5
If your sensor is in Esphome, it should get nan. How you update your sensor?
MateoMX
(Mateo Mx)
August 18, 2024, 9:08pm
6
No, the sensor seems to retain the last good value that was set. When modbus communication is lost, the value does not get set to nan or unknown…
You can see my config below…
interval:
- interval: 5s
then:
lambda: |-
ESP_LOGD("rx", "Interval value is %f", id(true_ma).state);
uart:
- id: pressure_uart
tx_pin: GPIO33
rx_pin: GPIO18
baud_rate: 19200
parity: NONE
stop_bits: 1
data_bits: 8
modbus:
- id: modbus2
flow_control_pin: GPIO16
uart_id: pressure_uart
disable_crc: False
modbus_controller:
- id: pressure_rx
command_throttle: 400ms
address: 0x14
modbus_id: modbus2
update_interval: 1s
setup_priority: -20
sensor:
- platform: modbus_controller
address: 0
name: true_ma
id: true_ma
device_class: current
unit_of_measurement: "mA"
modbus_controller_id: pressure_rx
register_type: holding
value_type: U_WORD
accuracy_decimals: 1
lambda: |-
ESP_LOGD("rx", "Doing something" );
return x;
filters:
- multiply: .1
on_value:
then:
- lambda: |-
ESP_LOGD("rx", "Sensor value is: %f", x );
- component.update: true_pressure
When I interrupt the modbus communication I see the following in the logs…
[15:53:53][D][rx:251]: Doing something
[15:53:53][D][rx:258]: Sensor value is: 4.000000
[15:53:54][D][rx:251]: Doing something
[15:53:54][D][rx:258]: Sensor value is: 3.900000
[15:53:55][D][rx:251]: Doing something
[15:53:55][D][rx:258]: Sensor value is: 4.000000
[15:53:56][D][rx:251]: Doing something
[15:53:56][D][rx:258]: Sensor value is: 4.000000
[15:53:57][D][rx:029]: Interval value is 4.000000
[15:53:58][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:53:58][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:53:59][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:53:59][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:53:59][W][modbus_controller:027]: Modbus device=20 set offline
[15:54:00][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:54:01][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:54:01][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:54:02][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:54:02][D][rx:029]: Interval value is 4.000000
[15:54:03][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:54:03][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:54:04][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
[15:54:05][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=0 count=1
[15:54:05][W][modbus_controller:179]: Duplicate modbus command found: type=0x3 address=7 count=1
As you can see from these logs… The lambda’s within the sensor: config are only being fired if the modbus communication is working well.
The interval lambda always fires, but while the modbus is faulty, the sensor retains the last know good value.
Karosm
(Karosm)
August 18, 2024, 9:28pm
7
I have to believe you. That’s ridiculously stupid.
What if you log data instead of x ?
I don’t have any modbus on my hands now.
MateoMX
(Mateo Mx)
August 19, 2024, 11:21am
8
Yeah, it does seem a little strange, right? Do you think it’s worth me raising this as an issue on github?
Karosm
(Karosm)
August 19, 2024, 1:04pm
9
MateoMX:
on_value:
then:
Maybe it’s not updating because it’s not considered “value”
MateoMX
(Mateo Mx)
August 19, 2024, 3:53pm
10
It’s strange because the ModBus docs suggest that the Lamba: section is processed on every update and you can use this to check for NAN, but as you can see as soon as the device is marked offline, that LOGD is not being fired.
Karosm
(Karosm)
August 19, 2024, 6:35pm
11
On_value lambda might not work if there is no “value”, but that before…
What about
lambda: |-
ESP_LOGD("rx", "Doing something" , x );
jeffmunk
(Jeffmunk)
October 26, 2024, 3:24am
12
Did you figure this out? Having a similar problem…
aleyush
(Alexander)
November 7, 2024, 1:07pm
13
You can try to watch last_reported.
The State object is now always updated and an event is always fired when an integration sets the state of an entity, regardless of any change to the state or a state attribute. This is implemented by adding a new timestamp, State.lastreported and a...