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

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:

  - alias: flora_2_remove_unavailable_values
    initial_state: true   
      - 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: template
      value_template: >-
        {{ trigger.to_state.state not in ['unavailable', trigger.from_state.state] }}
      service: mqtt.publish
        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.

long time no see :wink:

I’d suggest several things

  1. replace mqtt.publish with persistent_notification.create and see what’s going on (if any)
  2. tweak your condition a little bit
value_template: >
  {{ trigger.to_state.state and trigger.to_state.state not in ['unknown', 'unavailable', trigger.from_state.state] }}

Hello, unusual times, indeed :slight_smile:

It seems that the automation was correct and worked in the initial form, but I haven’t restarted HA since last week and I made some frontend changes during the weekend (after which a restart was probably necessary).

so it just required a good old restart?
good for you!
hope that’s the major problem in your life atm :wink:

I see you have that sun position custom component. Very nice. I’d really like to add it but it is still not compatible with home assistant (core / hassio).

The only current problem is that the plants are not getting sufficient light :slight_smile:

I think it needs Moon sensor, Sun2 custom component ( and Sun card ( (both of them from HACS, I haven’t added any custom component manually lately).

1 Like

Ah, I was actually thinking of this one: Polar plot of the Sun, Moon, a few planets, and the solstices

Might give the one you have a go.

1 Like

I wouldn’t say so looking at your weather forecast card.
Btw, what is the media player doing at the bottom of the big Weather card? :wink:

I just need to find a better place for the plants and for me to have a beer :slight_smile:

It is an input select with the names of Chromecast audios/groups and then a script reading weather:

  alias: 'Weather forecast'
    - service: tts.google_translate_say
        entity_id: '{{ states.input_select.weather_read_media_player.state}}'
        message: >
          {{["Hello.","Good morning.","Greetings.","Hi."] | random}} It's currently {{states.sensor.dark_sky_summary.state}} and {{states.sensor.dark_sky_temperature.state|round}} degrees. Today will be {{states.sensor.dark_sky_hourly_summary.state|replace(".", "")}}, highs: {{[0].temperature|round}}, lows: {{[0].templow|round}} Maximum precipitation intensity: {{states.sensor.dark_sky_precip_intensity.state | round(2)}} mm.

I will update those too with button cards (just completed in the weekend recreating quite a lot of scenes with the MiLight bulbs similar to Hue Scenes and I will overhaul some other parts of the frontend) but I currently struggle mighty with the horizontal alignment of cards as some of them are conditional and constantly varying in vertical size. Just hope that HA version 0.108 will improve things.

1 Like

Haven’t seen it until now, it looks great. I think I will try it on the non-hassio install