How to use offset properly?

I want to have my coffee started before i wake up.
Is it possible to do that with offset?

This is what i tried but did not work.

- alias: 'Kaffe'
  initial_state: true
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
    offset: "-00:20:00"
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_boolean.coffeornot
    state: 'on'
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Hemma'
  action:
  - service: homeassistant.turn_on
    entity_id: 
    - switch.net_perkulator
    - switch.perkulator

That would not work indeed as your value template would return true or false (which is good for triggers), but the offset can’t be applied to a true/false statement.
Can you tell me what your states.sensor.alarm_time.state returns?
Based on what you have there I can get you a formula for your value_template…


assuming your alarm_time is formatted in “hh:mm”, try and replace your code as such:

- alias: 'Kaffe'
  initial_state: true
  trigger:
    platform: template
    value_template: '{{ ((as_timestamp(now()) +1200 ) | timestamp_custom('%H:%M')) == states.sensor.alarm_time.state }}'
  condition:
  - condition: state
    entity_id: group.all_devices
    state: home
  - condition: state
    entity_id: input_boolean.coffeornot
    state: 'on'
  - condition: state
    entity_id: input_select.status_hemma
    state: 'Hemma'
  action:
  - service: homeassistant.turn_on
    entity_id: 
    - switch.net_perkulator
    - switch.perkulator

I’ve not tried it with a real instance of HA, so I’d probably add a condition that your automation hasn’t already ran in the last 2 min to make sure it doesn’t loop for a whole minute

    - condition: template
      value_template: '{{ (as_timestamp(now()) - as_timestamp(states.automation.kaffe.attributes.last_triggered | default(0)) | int > 120)}}'

If your automation doesn’t trigger, move your the value_template from your trigger in your conditions and set the trigger to be:

  trigger:
    - platform: time
      minutes: '/1'
      seconds: 0

so it runs every minute

Here is the sensors:

# time
  - platform: time_date
    display_options:
    - 'time'
    - 'date'
    - 'date_time'
    - 'time_date'
    - 'time_utc'
    scan_interval: 30

  - platform: template
    sensors:
      alarm_time:
        friendly_name: 'Alarmtid'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'

Am i thinking wrong?
To have the trigger 20 minutes before alarm
{{ ((as_timestamp(now()) +1200 )
Should be
{{ ((as_timestamp(now()) -1200 )

Actually i would like to use the same input_number values as in this sensor:

  - platform: template
    sensors:
      alarm_time:
        friendly_name: 'Alarmtid'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.alarmhour") | int, states("input_number.alarmminutes") | int) }}'

Would it be an option to instead create another sensor using the same input number selections but with an offset of minus 20 minutes?

if you want your alarm to come 20 mins early, you need to add 20 min to now() and check if now + 20 min = your alarm time. (as now + 20 min is your alarm - min 20) :wink:

ok try and replace the value template with this:

'{{ (((as_timestamp(now()) +1200 ) | timestamp_custom('%H')) == states.input_number.alarmhour.state ) and (((as_timestamp(now()) +1200 ) | timestamp_custom('%M')) == states.input_number.alarmminutes.state )  }}'

But it’s starting to get quite complex…

I get an error with that code

bad indentation of a mapping entry at line 688, column 80:
… ()) +1200 ) | timestamp_custom(’%H’)) == states.input_number.ala …

sorry the code was for the automation. It takes into account the offset of 20 min and then compare the time (hour and min) to make sure it matches the alarm…

I got the same error on the automation and having same indentation as other value_templates

I am using the following for my automation to start half an hour before the alarm clock:

   trigger:
     platform: template
     value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_less_30.state }}'

And this is the template that takes off 30 minutes:

     alarm_clock_time_less_30:
       value_template: >-
         {%if (states("input_number.alarm_clock_minutes") | int >= 30) %}
           {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minutes") | int -30) }}
         {% else %}
           {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minutes") | int +30) }}
         {% endif %}

Hope this helps :slight_smile:

1 Like

you need to check you code and indentations then. I’ve just checked the value_template formula again and there are no typo or errors.
Restore to a previous saved time, then add items bit by bit to see when it fails.

Thank you both for helping :slight_smile:

1 Like

I tried to amend this to be only 5 minutes and it isn’t working.
Any suggestions?

I adjusted it to be 45 minutes and that works fine. if you sum the first and the second part it needs to be 60.

         {%if (states("input_number.alarm_clock_minutes") | int >= 5) %}
           {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int, states("input_number.alarm_clock_minutes") | int -5) }}
         {% else %}
           {{ "%0.02d:%0.02d" | format(states("input_number.alarm_clock_hour") | int -1, states("input_number.alarm_clock_minutes") | int +55) }}
         {% endif %}```
1 Like

I must be an idiot… But where do I place the template? In a new script, automation…? I’ve tried several things, but I can’t get it to work

Hardly :slight_smile:

The one with “trigger” goes into an automation. The other blob goes into a sensor (sensor template).

Let me know if you figured it out with the above pointers, if not I will write a bit more when I am done with work.

Not quiet. This is what i’ve got

Sensors

sensor:
  - platform: template
    alarm_clock_time_less_30:
      value_template: >-
        {%if (states("input_number.alarmhour") | int >= 30) %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarmminutes") | int, states("input_number.alarmhour") | int -30) }}
        {% else %}
          {{ "%0.02d:%0.02d" | format(states("input_number.alarmminutes") | int -1, states("input_number.alarmhour") | int +30) }}
        {% endif %}

The automation is as

- id: '1092782611721'
  alias: Wake Me Up
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_clock_time_less_30.state}}'
  condition:
    condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        entity_id: input_boolean.alarmweekday
        state: 'on'
      - condition: time
        weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: state
      entity_id: input_boolean.alarmweekday
      state: 'off'
  action:
    service: notify.notify
    data_template:
      message: 'Test wekker'
      title: ''

The input for the alarm is

alarmhour:
  name: Hour
  icon: mdi:timer
  min: 0
  max: 23
  step: 1
alarmminutes:
  name: Minutes
  icon: mdi:timer
  min: 0
  max: 59
  step: 5

I can’t get the sensor to show up in entities and also the alarm doesn’t notify me half an hour before…
What I do see in the entities however is this:

[sensor.alarm_time] | 15:30 | friendly_name: Time

Perhaps that’s something I can work with?

You need to pay more attention to the information you’re using.
And consult with the official HA docs - take a look.

nope.

and where did you get your code from - this does not make sense to me

as it’s

Here’s your template sensor that should work as expected:

sensor:
  - platform: template
    sensors:
      alarm_clock_time_less_30:
        value_template: >-
          {% set alarmhour = states("input_number.alarmhour") | int %}
          {% set alarmminutes = states("input_number.alarmminutes") | int %}
          {% if alarmminutes >= 30 %}
            {% set alarmminutes = alarmminutes - 30 %}
          {% else %}
            {% set alarmhour = alarmhour - 1 %}
            {% set alarmminutes = alarmminutes + 30 %}
          {% endif %}
          {{ "{:02d}:{:02d}".format(alarmhour, alarmminutes) }}
1 Like