I want to send an MQTT message when the media player (in my case an IKEA Symfonisk, which is a Sonos at heart) changes what it plays. I have the change from paused to playing down:
trigger:
- platform: state
entity_id: media_player.stue
to: 'paused'
from: 'playing'
But if I try my regular states trigger:
trigger:
- platform: state
entity_id: media_player.stue
The problem is that this of course triggers on any state change, so it triggers because the playing minutes changes. So I get the changes every five seconds. Is there a way to trigger only on the state:
I tried to adapt this to another media player, but it seems to work a bit too good, because it triggers on stuff that isn’t an input change, like volume. So I tried to limit it to when the actual input changes, but I can’t get that to work:
#Sende beskjed nĂĄr Marantz pĂĄ soverom 2 i 3. bytter inngang til TV
- id: '1486677632'
alias: Beskjed nĂĄr Marantz pĂĄ soverom 2 i 3. bytter inngang til TV
trigger:
- platform: state
entity_id: media_player.soverom_2_i_3
to: "TV"
action:
- data:
payload: TV
topic: eg/Soverom 2 i 3. inngang
service: mqtt.publish
I tried with tv as well, in case it didn’t like the capital letters, but that didn’t hep either. Do I need another specification there for this to work? If I don’t use the “to” it works.
What states does media_player.soverom_2_i_3 take on? Does it actually change to "TV"?
You said “I tried to limit it to when the actual input changes”. How does media_player.soverom_2_i_3 change when an input changes? Does it state change? Does an attribute change, and if so, which one and what does it change to? (I don’t use the media_player component, so I can’t guess for you.)
Thanks for answering! Yes, the attribute {{states.media_player.soverom_2_i_3.attributes.source}} changes. And that’s the input names, like Blu-Ray, TV, DVD and so on.
Final thing in this one (I think): I have made a similar one for the state change to “off” and “on”. “Off” is no problem, that’s only triggered when the receiver is actually turned off. But “on” is triggered when an input is changed too, and I would like to avoid that. This is the code:
- id: '148667877762332'
alias: Beskjed nĂĄr Marantz pĂĄ soverom 2 i 3. blir slĂĄtt pĂĄ
trigger:
platform: template
value_template: "{{ is_state('media_player.soverom_2_i_3', 'on') }}"
action:
- data:
payload: Paa
topic: eg/Soverom 2 i 3. power
service: mqtt.publish
Is it possible to do the value template so that it only triggers on the change when “off” goes to “on”, but not when there’s no change, and on goes to “on”? I was thinking of conditions, but I can’t find any condition that covers a value change.
A template trigger will be (re)evaluated every time the state (state or attribute) of the referenced entity changes. However, once it has triggered (i.e., the expression evaluates to true), it won’t trigger again until the expression evaluates first to false, and then again to true.
So, not sure why this last automation you show would trigger if the state stays 'on', but the source attribute changes. That should not be possible. I’m guessing the state is changing temporarily to something else and then back to 'on'. You should check the logs.
BTW, if you want to trigger on an entity’s state change, you can certainly do that with a template trigger like you did, but typically people use a state trigger for that:
trigger:
platform: state
entity_id: media_player.soverom_2_i_3
to: 'on'
Although this is the more typical way to trigger on that event, it is effectively equivalent to what you wrote. Specifically, I’m not saying changing to this trigger will solve your problem. It shouldn’t.
You need to find out what is happening with the state (state & attributes) of media_player.soverom_2_i_3 first. Otherwise you won’t be able to solve your problem.
OK, thanks! I think you’re right about that. It probably changes to off for a fraction of a second when changing zones, which means that it’s a quirk in the receiver. So I guess that would involve a bit more coding, which I can do in EventGhost, which is the program that receives the signals. I’ll just add an if that checks if the new power state is the same as the previous.
Another thing: It seems like Home Assistant is polling the receiver every ten seconds, since it takes from instantly and up to ten seconds before a change is noted by both the script and the GUI. If I set the polling to every 2 seconds, would that be something that took up much ressources? It’s on a wired network, not wireless, and I’m only running this script plus an MQTT server on that Pi.
Edit: By polling I guess I mean “scan_interval”.
Edit 2: I took the chance and set it to 2, it seems like the scan is done about every 3 seconds, that may be a flooding thing in the receiver, or in Home Assistant. But the CPU usage on the Pi didn’t change at all, it’s still around 2-5 %.
Edit 3: Oh, and the states trigger was the first thing I tried for this, but as I said earlier in the thread that triggered an “input changed” on any change on the receiver, not just tha particular state’s change. But the template trigger only triggers when the input is actually changed.