MODBUS on_error -or- on_device_offline do X

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?

Sorry to bump this… Are there any modbus experts out there?

if (isnan(id(your_comressor_sensor).state)) {id(your_relay).turn_off();}

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.

If your sensor is in Esphome, it should get nan. How you update your sensor?

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.

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.

Yeah, it does seem a little strange, right? Do you think it’s worth me raising this as an issue on github?

Maybe it’s not updating because it’s not considered “value”

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.

On_value lambda might not work if there is no “value”, but that before…

What about

lambda: |-
      ESP_LOGD("rx", "Doing something" , x );

Did you figure this out? Having a similar problem…

You can try to watch last_reported.