Christmas rules in 1 autimation

Hello all,

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 :thinking:

Regardless, we like a challenge :rofl:
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' 

The christmas url is:
https://21633.live.streamtheworld.com/SRGSTR08.mp3

What i want is the following in a year:
from december 27 till november 23 the normal url
from 23 november till december 3 the christmas url
only on december 4 and 5 this url:
http://playerservices.streamtheworld.com/api/livestream-redirect/SRGSTR28.mp3
from december 6 till december 27 the christmas url

hope this make sense :wink:

Try : -

- 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 : -

  1. The tests go from highly specific to the more generalised
  2. 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
  3. 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.
  4. 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.
  5. I can’t test this as I don’t have the entities so you will have to report back any issues.
  6. MOST IMPORTANT This requires ‘sensor.date’ to be set up in your configuration (see : https://www.home-assistant.io/integrations/time_date/ )
  7. 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.
  8. 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 :rofl:

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

1 Like

Tip:

You can replace these three lines:

          {% set today = states('sensor.date') %}
          {% set date = today [8:10] | int %}
          {% set month = today [5:7] | int %}

with these two:

          {% set date = now().day %}
          {% set month = now().month %}

Yes, I thought about that but the way I did it : -

  1. Requires time date sensors to be implemented (so much use generally, they are good to have)
  2. Introduces text truncation (another good skill to have)
  3. It casts to integer so it reminds people that you need to test how ‘objects’ are dimensioned and cast appropriately
  4. Though (as yours shows) mine takes a small detour, it is the better scenic route for a newbie learning templating
    :man_shrugging:

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:

  1. 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 :+1: 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
:rofl:

1 Like

That’s true. Carry on, helpful tour guide! :slight_smile:

1 Like