Need template to automate this volume increase

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 .

trying to set this up, this might be the most crucial bit, it would need to follow the template.

checking Timer - Home Assistant though shows no option for a template… which leaves me in the same situation.

or would

timer:
  radio_timer_delay
    name: Radio timer delay
    duration: {{states('input_number.radio_timer_delay')|int}}

do it? If I see this: Setting duration of timer with data_template not working

a timer with a fixed duration is created, and then started with a template in the duration section. Would these be added somehow, or does the template replace the fixed duration…? a bit confusing this is

like:

  - alias: 'Increase volume loop'
    id: 'Increase volume loop'
    initial_state: 'off'
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.radio_timer_delay
#      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: timer.start
        data_template:
          entity_id: timer.radio_timer_delay
          duration: >
            {{states('input_number.radio_timer_delay')|int}}
      - 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 %}

  - alias: 'Start radio delay timer'
    id: 'Start radio delay timer'
    initial_state: 'off'
    trigger:
      platform: state
      entity_id: input_number.radio_timer_delay
    condition:
    action:
      - service: timer.start
        data_template:
          entity_id: timer.radio_timer_delay
          duration: >
            {{states('input_number.radio_timer_delay')|int}}

timer:
  radio_timer_delay:
    name: Radio timer delay
    duration: 30

started by this script:

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

new post for clarity: its working!

script:
  radio_timer_delay:
    alias: Radio timer delay
    sequence:
      - service: timer.start
        data_template:
          entity_id: timer.radio_timer_delay
          duration: >
            {{states('input_number.radio_timer_delay')|int}}
automation
  - alias: 'Increase volume loop'
    id: 'Increase volume loop'
    initial_state: 'off'
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.radio_timer_delay
    condition:
      - condition: template
        value_template: >
          {{is_state('input_boolean.snooze','on')}}
      - condition: template
        value_template: >
          {{states('sensor.alarmclock_radio_volume')|float < 1 }}
    action:
      - service: script.radio_timer_delay
      - 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 %}

timer:
  radio_timer_delay:
    name: Radio timer delay
    duration: 30

all set to fire with this little script:

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

made a kill_switch to rule them all:

  - alias: 'Alarm clock off'
    id: 'Alarm clock off'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id:
        - input_boolean.snooze
        - input_boolean.alarmclock_wd_enabled
        - input_boolean.alarmclock_we_enabled
        - input_boolean.lullaby_radio
        - input_boolean.wakeup_radio
      to: 'off'
    condition:
      condition: template
      value_template: >
        {{states('media_player.googlehome_master_bedroom') == 'playing'}}
    action:
      - service: media_player.turn_off
        entity_id: media_player.googlehome_master_bedroom
      - service: timer.cancel
        entity_id: timer.radio_timer_delay
      - service: automation.turn_off
        entity_id: automation.increase_volume_loop

thanks @123 for inspiring me. Was able to cut your second bullet :wink: KISS, as possible.

I’m looking to do something similar.
Im not sure if i should use timer or not.

What i have so far.
2 input booleans, one for wekkermode and one for wekkeractief
these automations


- id: 'wekker_slaapkamer_detecteer_tijd'
  alias: Wekker slaapkamer tijd
  description: 'Detecteer wekker tijd'
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('sensor.wekker_tijd_slaapkamer') }}"
  condition:
    condition: state
    entity_id: input_boolean.wekkermode_slaapkamer
    state: 'on'
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.wekkeractief_slaapkamer

- id: 'wekker_slaapkamer_geactiveerd'
  alias: 'Wekker slaapkamer geactiveerd'
  trigger:
    platform: state
    entity_id: input_boolean.wekkeractief_slaapkamer
    from: 'off'
    to: 'on'
  action:
    service: timer.start
    entity_id: timer.wekker_slaapkamer

When the wekker(alarm) is active… i want to have a button “turn off” or “snooze”
When the alarm goes off i want my hub to play music and turn on nightlight.
I think i m gonna add this in a script and as condition have it my boolean alarm active
However the music should have the sound go up from 0 to for example 5 (google hub)
and stay at 5 until i press snooze or off

Pressing of isnt hard, but snooze is

Need some help here…
Especially to turn music up from 0 to 5 and keep 5
(in steps)
And how to snooze and then it redoes the same thing

Hope its clear

using this nowadays:

  play_wakeup_radio:
    alias: Play wakeup radio
    mode: restart
    sequence:
      - service: media_player.volume_set
        data:
          entity_id: >
            {{states('sensor.wakeup_radio')}}
          volume_level: >
            {{states('input_number.wakeup_radio_volume')}}
      - service: media_player.play_media
        data:
          entity_id: >
            {{states('sensor.wakeup_radio')}}
          media_content_id: >
            {{states('sensor.radio_station_wake_up')}}
          media_content_type: music
      - delay: >
          {{states('input_number.increase_volume_delay')|int}}
      - alias: Increase volume AS LONG AS the conditions are true
        repeat:
          while:
            - condition: state
              entity_id:
                - input_boolean.increase_volume
                - input_boolean.snooze
              state: 'on'
            - >
                {{states(states('sensor.wakeup_radio')) == 'playing'}}
            - >
                {{states('sensor.wakeup_radio_volume')|float < 1}}
          sequence:
            - service: media_player.volume_set
              data:
                entity_id: >
                  {{states('sensor.wakeup_radio')}}
                volume_level: >
                  {% set level =
                     states('sensor.wakeup_radio_volume')|float +
                     states('input_number.increase_volume')|float %}
                  {% if level < 1 %} {{level|round(2)}}
                  {% else %} 1
                  {% endif %}
            - delay: >
                {{states('input_number.increase_volume_delay')|int}}
      - delay: >
          {% set delay = states('input_number.snooze_delay')|int * 60 %}
          {% set spent = (now() - state_attr('script.play_wakeup_radio','last_triggered')).total_seconds() %}
          {{ [0, delay-spent] | max }}

      - service: media_player.turn_off
        data:
          entity_id: >
            {{states('sensor.wakeup_radio')}}

please see if this does what yo want?