Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95365

Publish last valid value of a sensor to a MQTT topic (to set another sensor)

$
0
0

@Petrica wrote:

Trying to send last valid values from a sensor to a MQTT topic in order to set new sensors, as the original sensors (ESPHome) get unavailable state from time to time:

automation:
  - alias: flora_2_remove_unavailable_values
    initial_state: true   
    trigger:
      - platform: state
        entity_id: sensor.flora_air_temperature_2
      - platform: state
        entity_id: sensor.flora_light_intensity_2
      - platform: state
        entity_id: sensor.flora_soil_fertility_2
      - platform: state
        entity_id: sensor.flora_soil_moisture_2
    condition:
      condition: template
      value_template: >-
        {{ trigger.to_state.state not in ['unavailable', trigger.from_state.state] }}
    action:
      service: mqtt.publish
      data_template:
        topic: "flora/{{ trigger.entity_id.split('.')[1].split('_')[2] }}"
        payload: "{{ trigger.to_state.state }}"

the automation, although fires (last_triggered is updated each time the original sensors get modified values, so the trigger works), it doesn’t publish anything (and there isn’t any error in the log).

In templates {{ states.sensor.flora_soil_fertility_2.entity_id.split(".")[1].split('_')[2] }} correctly gets fertility, thus the action should work too.

The original sensor updates several time per minute (when not sending unavailable state) and the values are almost always different than previous.

Posts: 11

Participants: 3

Read full topic


Viewing all articles
Browse latest Browse all 95365

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>