I have seen already many examples here on the forum but that involves 2 automation.
I have a script with a mediastream url that plays music from a radio station and an automation that triggers this script based on a sensor.
1 want in December that the automation use a different url (Christmas stream) then rest of the year.
The examples I see is always 2 automations for this. I’m looking to put it in one automation. This can be done in 2 things:
by changing the automation
Or adjust the script
My question is: can it be done in one? Or must I create 2 automations?
There does seem to be a compulsion to reduce automations / scripts wherever possible, I understand this compulsion (I have it myself)
But if ‘someone’ gives you a single automation (we can even dispense with the script) will you understand it ? Be able to maintain it ? Adapt it in future ? What about if there are future breaking changes and it needs to be rewritten ?
I’m not saying all (or even any) of the above apply to you but you need to consider these things.
Most of all, I’d be wary of using something that you don’t know how it works
Regardless, we like a challenge
Post what you currently have, and the url for your Christmas version and we’ll see what can be done
@Mutt: I understand. what youmean. I’m trying most of the things myelf by looking and altering other scripts. I can build the 2 automations myself but struggling to put it in 1 automation.
I have in automation.yaml:
- id: '1604483239660'
alias: Badkamer deur open - Radio aan
description: Badkamer deur open - Radio aan
trigger:
- platform: state
from: 'off'
to: 'on'
entity_id: binary_sensor.openclose_badkamer_deur
condition:
- condition: device
device_id: ef08935a7ede5fb51dbaf32fa1787865
domain: media_player
entity_id: media_player.badkamer_speaker
type: is_off
- condition: time
after: 07:30:00
before: '22:00:00'
action:
- service: script.badkamer_speel_radio
data: {}
mode: single
And in scripts.yaml:
badkamer_speel_radio:
alias: Speel radio af op de badkamer
sequence:
- service: media_player.volume_set
data:
entity_id: media_player.badkamer_speaker
volume_level: '0.15'
- service: media_player.play_media
data_template:
entity_id: media_player.badkamer_speaker
media_content_id: http://playerservices.streamtheworld.com/api/livestream-redirect/SRGSTR24.mp3
media_content_type: 'audio/mp4'
- id: '1604483239660'
alias: Badkamer deur open - Radio aan
mode: single
description: Badkamer deur open - Radio aan
trigger:
- platform: state
entity_id: binary_sensor.openclose_badkamer_deur
from: 'off'
to: 'on'
condition:
- condition: device
device_id: ef08935a7ede5fb51dbaf32fa1787865
domain: media_player
entity_id: media_player.badkamer_speaker
type: is_off
- condition: time
after: '07:30:00'
before: '22:00:00'
action:
- service: media_player.volume_set
data:
entity_id: media_player.badkamer_speaker
volume_level: '0.15'
- service: media_player.play_media
data_template:
entity_id: media_player.badkamer_speaker
media_content_id: >
{% set today = states('sensor.date') %}
{% set date = today [8:10] | int %}
{% set month = today [5:7] | int %}
{% if month == 12 and (4 <= date <= 5) %}
'http://playerservices.streamtheworld.com/api/livestream-redirect/SRGSTR28.mp3'
{% elif (month == 11 and date >= 23) or (month == 12 and date <= 27) %}
'https://21633.live.streamtheworld.com/SRGSTR08.mp3'
{% else %}
'http://playerservices.streamtheworld.com/api/livestream-redirect/SRGSTR24.mp3'
{% endif %}
media_content_type: 'audio/mp4'
I was going to use ‘choose’ but quite frankly it doesn’t need it as only one line changes
Note that : -
The tests go from highly specific to the more generalised
You should be able to work out the relevant dates from the numbers used, and thus be able to tweak it if your needs change
I note from the format of your original that you use the GUI editor. This is manually written and I’m not sure if the GUI is ‘caught up’ to this level of templating. So you ‘may’ just have to cut and paste this in.
If it goes through a minor edit in the GUI it will translate it to JSON and then back to yaml so it ‘may’ chew it up a bit, but it should still work.
I can’t test this as I don’t have the entities so you will have to report back any issues.
I wouldn’t write the rest of my automations like this but I’ve tried to leave your original as intact as possible so a) you can recognise it b) see the bits that ‘needed’ to change.
mode: single is the default (so isn’t strictly necessary) but I left it in and moved it to the top (still the same location in yaml hierarchy (indentation) ).
Edit: your middle stream (the shortest one) comes back as forbidden for me ???
And the first one is ‘really’ irritating
Edit2: I forgot to mention that in your original automation, your spacing was off towards the end (I’ve never known the GUI editor to do this, so I’m not sure how that crept in.
Yaml spacing is always a multiple of ‘two spaces’. It threw an error in my editor, but is corrected in my code above. You will sometimes get obscure errors and 9 times out of 10 it’s the spacing.
Edit3: I’d hazard a guess that the 4th and 5th of December is when you wife’s mother is staying with you
Thank you so much @Mutt !!
The only thing i needed to change was remove the ’ ’ before and after the cast url.
I checked the script and did check line for line if i understand the flow and that i did.
You said:
I wouldn’t write the rest of my automations like this but I’ve tried to leave your original as intact as possible so a) you can recognise it b) see the bits that ‘needed’ to change.
Can you tell me as learning curve what then can be better?
and:
I’d hazard a guess that the 4th and 5th of December is when you wife’s mother is staying with you
No its a dutch tradition:
again thank you so much you helped me understanding the script
That’s very hard to say, I’ve never used the gui editor ‘in anger’ (ie to create an automation I actually use) but I’ve been doing this more than 3 years now (long long before there was a GUI editor)
The gui editor is getting better and better, but it’s still not there yet. And when it comes to templates I don’t think it ever will as its a bit like writing a novel by assembling ‘bits’ from other books.
The permutations are endless and so are the pitfalls
All I can recommend is reading what the gui editor gives you, to see what you get, then whenever you have a spare hour, go back and read them again, and again, and again.
Templates are infinitely flexible, you can do ANYTHING with a template, you just have to read them, see what you can interpret, ask questions, use the template editor (under developers tools (if you are running in advanced mode)) and read forum questions, see how they are answered and later try to solve some yourself
Regarding the ’ ’ - that’s what I meant about not being able to test it myself.
The important bit is to give you a push along the way, you managed to debug it yourself so for that.
Keep trying - you’ll get there
Re the music for the 4th & 5th - a fellow member DM’d me to say “if that was playing in his bathroom, he’d take a dump and get out of there as quickly as possible”.
The Icelandic’s have multiple Santa’s including one who plays tricks mischievously
Each to their own