Need template to automate this volume increase

HI,

testing a silly script like below. Searched the community how this can be written intelligently… Somehow all the answers are either in Nodered (which I don’t use) or not simple enough to replace with. How should I rewrite this, so it iterates 0.5 each delay?

  wakeup_radio:
    alias: wakeup radio
    sequence:
      - alias: Set Volume
        service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.01'
      - service: script.play_wakeup_radio
      - delay:
          minutes: 2
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.05'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.10'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.15'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.20'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.25'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.30'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.35'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.40'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.45'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.50'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.55'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.60'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.65'
      - delay:
          minutes: 1
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.70'
  - delay: '00:02:00'

that’s not really an answer to my question… merely another way of writing the delays. Had nasty experience with that notation before and always use

      - delay:
          minutes: 1

Im looking for some kind of loop, to add or subtract (in case of my lullaby script…) from the volume, each set time delay.

My bad then. I thought your script wasnt working, and also I wasnt aware there was another way of writing the delay…

So I guess you need a template here to make it work, but I am not the guy for that. Sorry.

thanks for having a look, no worries.

I know have a separate automation to set the volume according to an input_number. I could probably use that also, next to a for loop setting that input number. If it can’t be done in 1 move of course…

@petro what would be easiest you think?

From a quick search on the jinja documentation, I found this:
http://jinja.pocoo.org/docs/2.10/templates/#list-of-global-functions

Could this be your case? It looks like range is what you need.

A Python script. Otherwise the way you have it written is the easiest.

thanks!
considering I only need to worry about the volume setting (have another automation applying the volume) I would have hoped to do something like:

  - alias: 'Increase volume loop'
    id: 'Increase volume loop'
    trigger:
      platform: time #(_pattern)
      minutes: '/1'
      seconds: 00
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.snooze','on')}}
    action:
      service: media_player.set_volume
      entity_id: media_player.googlehome_master_bedroom
      data_template: 
        volume_level: >
          {{ states('input_number.radio_volume')|float + 0.5 }}

and then add this script to the main script:

  wakeup_radio:
    alias: wakeup radio
    sequence:
      - alias: Set Volume
        service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.01'
      - service: script.play_wakeup_radio
      - script.increase_volume_loop

would even be cooler with a template for time pattern and volume_increase, based on input_number.

I don’t see why that wouldn’t work. You’d just need to have an automation that turns it off after x minutes.

that, or my input_boolean.

Or just say: ‘Ok google, stop’:-))

great, let me see if I can make it, and Ill report back

Is this what you are looking for?
Even if it is I am not sure if it is any better than anything else suggested here. It’s just something I was playing with.

testing this now:

  - alias: 'Decrease volume loop'
    id: 'Decrease volume loop'
    initial_state: 'off'
    trigger:
      platform: time #(_pattern)
      seconds: >
        {{states('input_number.radio_timer_delay')}}
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.lullaby_radio','on')}}
    action:
      service: media_player.volume_set
      entity_id: media_player.googlehome_master_bedroom
      data_template: 
        volume_level: >
          {{ states('input_number.radio_volume')|float + states('input_number.decrease_volume')|float }}

  - alias: 'Increase volume loop'
    id: 'Increase volume loop'
    initial_state: 'off'
    trigger:
      platform: time #(_pattern)
      seconds: >
        {{states('input_number.radio_timer_delay')}}
    condition:
      condition: template
      value_template: >
        {{is_state('input_boolean.snooze','on')}}
    action:
      service: media_player.volume_set
      entity_id: media_player.googlehome_master_bedroom
      data_template: 
        volume_level: >
          {{ states('input_number.radio_volume')|float + states('input_number.increase_volume')|float }}

and turn on the automations in the calling scripts:

  wakeup_radio:
    alias: Wakeup radio
    sequence:
      - service: script.play_wakeup_radio
      - service: automation.turn_on
        entity_id: automation.increase_volume_loop

  play_wakeup_radio:
    alias: Play wakeup radio
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.01'
      - service: media_player.play_media
        entity_id: media_player.googlehome_master_bedroom
        data_template:
          media_content_id: >
            {{states('sensor.radio_station_wake_up')}}
          media_content_type: 'audio/mp4'

################

  lullaby_radio:
    alias: Lullaby radio
    sequence:
      - service: script.play_lullaby_radio
      - service: automation.turn_on
        entity_id: automation.decrease_volume_loop

  play_lullaby_radio:
    alias: Play lullaby radio
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: media_player.googlehome_master_bedroom
          volume_level: '0.4'
      - service: media_player.play_media
        entity_id: media_player.googlehome_master_bedroom
        data_template:
          media_content_id: >
            {{states('sensor.radio_station_lullaby')}}
          media_content_type: 'audio/mp4'

but I can’t get the automations to turn on. Could that be because the time trigger (still using the time, not time_pattern on 84.3) can’t use a template?

o that’s much more complex, will have to see if that would of use here too! very nice though, i havent used a loop like that in my setup yet. looks very nifty.

my first attempt didn’t fare well, some silly errors (not yaml but conceptual) and the unfortunate impossibility not to be able to template the time trigger, which is a real let down…

changed to this:

  - alias: 'Decrease volume loop'
    id: 'Decrease volume loop'
    initial_state: 'off'
    trigger:
      platform: time #(_pattern)
      minutes: '/3'
      seconds: 00
#      seconds: >
#        /{{states('input_number.radio_timer_delay')|int}}
    condition:
      - condition: template
        value_template: >
          {{is_state('input_boolean.lullaby_radio','on')}}
      - condition: template
        value_template: >
          {{states('sensor.alarmclock_radio_volume')|float > 0 }}
    action:
      service: media_player.volume_set
      entity_id: media_player.googlehome_master_bedroom
      data_template: 
        volume_level: >
          {% set level = 
             state_attr('media_player.googlehome_master_bedroom','volume_level')|float|round(2) - 
             states('input_number.decrease_volume')|float %}
          {% if level > 0 %} {{ level }}
          {% else %} 0
          {% endif %}

  - alias: 'Increase volume loop'
    id: 'Increase volume loop'
    initial_state: 'off'
    trigger:
      platform: time #(_pattern)
      minutes: '/1'
      seconds: 00
#      seconds: >
#        /{{states('input_number.radio_timer_delay')|int}}
    condition:
      - condition: template
        value_template: >
          {{is_state('input_boolean.snooze','on')}}
      - condition: template
        value_template: >
          {{states('sensor.alarmclock_radio_volume')|float < 1 }}
    action:
      service: media_player.volume_set
      entity_id: media_player.googlehome_master_bedroom
      data_template: 
        volume_level: >
          {% set level = 
             states('sensor.alarmclock_radio_volume')|float + 
             states('input_number.increase_volume')|float %}
          {% if level < 1 %} {{ level }}
          {% else %} 1
          {% endif %}

as you can see I’ve built-in a few conditional safeguards, and an intermediary volume sensor to make the automation more readable.
Increase works now, have to test my sleep scripts, but won’t do so till tonight…

37

Have you considered using a timer to trigger the automation?

You can use the timer’s services to start/stop it as well as change its duration (via input_number.radio_timer_delay).

not yet no, would that be possible?woudl be great!

I’d love to be able to edit the timing via the frontend, so if you could see a way in my script/automation to add that, you’d be most welcome :wink:

  • Create a timer and assign it a default duration.
  • Create an automation that sets the timer’s duration whenever input_number.radio_timer_delay changes.
  • Modify your volume increase/decrease scripts to trigger on an event. The event is when the timer finishes (timer.finished). The automations will need to restart the timer.
  • Modify your other automations that control when the volume should start increasing/decreasing (i.e. something needs to start the timer’s countdown using service timer.start).

ok thanks, will look in to that for sure, seems overly complicated though, if we could just as easily do:

    trigger:
      platform: time #(_pattern)
      seconds: >
        /{{states('input_number.radio_timer_delay')|int}}

I thought you might say that, so that’s why I didn’t bother posting full code. :wink:

The ‘complication’ is the addition of a timer and simple automation. You can revise the solution so that the timer is instructed to increment (or decrement) the volume after completing its cycle and then restart itself to run another cycle until it reaches a given number of repetitions or duration (or when it is halted). It’s basically the time_pattern design but with the ability to dynamically adjust the number of repetitions (using an input_number).

O please feel welcomed to do so, I have some rather nifty templates and constructs in my rather extended setup…
Its just I like to do things as simple as possible. If possible. Apparently what I want isn’t possible in a simple way at the moment, so I now skip it, using a hardcoded time_pattern.

If your setup would provide a templated one based on the input_number, I’d switch it in a split second.

that doesn’t sound to frightening :wink: Ill see what I can come up with, after re-reading what you’ve said .