'on_value' automations do not work with repeated values

I’m using the following homeassistant text sensor config on some esphome devices, to provide voice prompts for various automations:

text_sensor:
  - platform: homeassistant
    name: "ESPhome Voice"
    internal: true
    entity_id: input_text.esphome_voice
    on_value:
      then:
        - dfplayer.set_volume: 25
        - delay: 150ms
        - dfplayer.play_folder:
            folder: 1
            file: !lambda return atoi(x.c_str());

It’s simple but works great… for the most part. Unfortunately, if the entity happens to receive the same string twice, the automation doesn’t fire a second time (or 3rd-Nth if that happens). Seems like the text_sensor automation doesn’t work if it repeatedly gets the same string

I’m not sure if this is the intended behavior, but I need the automations to fire every time, even when last value = previous value. Seems like a trivial thing to do, but I haven’t run in to a need to do it with HA before.

Any and all help welcome and appreciated…

perhaps a copy of your automation?

That end of my setup is working fine, but in case I’m missing something… in node red the automations that change the text sensor look very simple like this (there are many similar branches like this for different prompts, which all feed into single text_input node at the end):

[{"id":"bbad6d8e.870e8","type":"api-call-service","z":"d6bde18e.b9016","name":"","server":"684e76d1.f36e78","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.esphome_voice","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":2330,"y":800,"wires":[[]]},{"id":"b385595f.afdb28","type":"change","z":"d6bde18e.b9016","name":"\"001\"","rules":[{"t":"set","p":"payload","pt":"msg","to":"001","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2150,"y":800,"wires":[["bbad6d8e.870e8"]]},{"id":"46e2ef93.4c023","type":"trigger-state","z":"d6bde18e.b9016","name":"","server":"684e76d1.f36e78","exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityid":"switch.front_yard_lights","entityidfiltertype":"exact","debugenabled":false,"constraints":[{"id":"hfnqlnm62r","targetType":"this_entity","targetValue":"","propertyType":"current_state","propertyValue":"new_state.state","comparatorType":"is","comparatorValueDatatype":"str","comparatorValue":"on"},{"id":"th9b7p525kc","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"unknown"},{"id":"wtaxzxd6k3","targetType":"this_entity","targetValue":"","propertyType":"previous_state","propertyValue":"old_state.state","comparatorType":"is_not","comparatorValueDatatype":"str","comparatorValue":"unavailabe"}],"constraintsmustmatch":"all","outputs":2,"customoutputs":[],"outputinitially":false,"state_type":"str","x":1910,"y":800,"wires":[["b385595f.afdb28"],[]]},{"id":"684e76d1.f36e78","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

The HA entity gets it’s state updated properly, and as I mentioned any time a DIFFERENT value is sent everything works fine. It only has issues if say, the same text is repeated. For example, say the sensor is at “002”, then changes to “001” (the voice prompt works), then a minute later another “001” (this voice prompt does not work). In perhaps most contexts within HA, automations only need to respond when a state changes value, but in this case it needs to respond to a new value even if it hasn’t actually changed.

I am sure I could make a hacky workaround by having my automations set the value to some invalid number after each run. That way HA would see a same text input as an actual change, from the invalid state. I’d like to avoid this… not sure if the dfplayer library etc would puke on an invalid file name? … plus I don’t like hacks if possible.

Perhaps, like home assistant automations it only operates on change.

Perhaps put a silent file in between the real files, very short.

1 Like

Now that is a workaround I can live with… solution earned. Thanks!

[update: While the marked solution does work…

The lightbulb went off in my head after I looked at my input_text configuration…

input_text:
  esphome_voice:
    name: ESPhome Voice
    initial: '100'

… and I recalled my mp3 filenames only range 000-050ish (for now :wink: ).

So these devices have been handling the invalid ‘100’ file after every reboot for some time, no problem (I had initial: '100' from day one). Therefore, I did some more brief testing to verify my theory… I am now pretty confident the dfplayer and related libraries handle invalid file names without issues (note it is <255, the max valid filename per dfplayer lib docs). So I simply added this snippet to my node red automations, and now I get a voice prompt every time regardless of the value:

[{"id":"ff445fb8.ee04d","type":"api-call-service","z":"4841ab33.0a7244","name":"set esphome_voice","server":"684e76d1.f36e78","version":1,"debugenabled":false,"service_domain":"input_text","service":"set_value","entityId":"input_text.esphome_voice","data":"{\"value\":\"{{payload}}\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":730,"y":80,"wires":[[]]},{"id":"78df30ea.4a30b","type":"change","z":"4841ab33.0a7244","name":"100","rules":[{"t":"set","p":"payload","pt":"msg","to":"100","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":80,"wires":[["ff445fb8.ee04d"]]},{"id":"5bedda50.19ac34","type":"delay","z":"4841ab33.0a7244","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":440,"y":80,"wires":[["78df30ea.4a30b"]]},{"id":"6b8d0c37.a109c4","type":"server-state-changed","z":"4841ab33.0a7244","name":"","server":"684e76d1.f36e78","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_text.esphome_voice","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"100","halt_if_type":"str","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":200,"y":80,"wires":[["5bedda50.19ac34"],[]]},{"id":"684e76d1.f36e78","type":"server","z":"","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

If the text_input changes to anything but ‘100’, it waits 5sec then sets it to ‘100’. The 5sec wait is adequate to cover my longest mp3 file. Otherwise, if you play your ‘invalid file’ too soon, it will cut off any mp3 file that is currently still playing.

1 Like