Time minus ten minutes

Any idea how I would use a template to take the time state of an input_datetime or whatever and subtract X minutes from it?

Example the time set is 7.30, template is time minus 10 minutes, output is 7.20.

Or time is set to 7.05, template is minus 20 minutes, output is 6.35

I suspect this shouldn’t be too complicated, but I can’t find anything about manipulating a predefined time, just manipulating system time.

Thanks in advance.

The following code adds an amount of time (in minutes) into the time of sunset using an input_number.

{{ (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%H:%M") }}

On the configuration of the input_number you can define min: -90 and max: 90. If you use a negative value the formula will subtract that time. Example:

If you set time to 7.30 and the input_number value is -10 you’ll get 7.20

Hope this helps.

2 Likes

Thanks, I’ll have a play later and let you know :+1:

Sorry it’s took me so long to get back to this.

I’m struggling to adapt this to what I’m trying to achieve. Do you have a Github repo so I can see this in action? Or can you post the full script/automation this is used in please?

Thanks in advance.

That’s ok. I have GitHub but indont keep my HA files there.

Right now I’m on my way to work, so only later today I can post the automation. Sorry…

No worries, whenever you get the chance, I’m very grateful :+1:

I’m back!

First things first: I’m using split configuration so everything is separated in different files and folders. Remember that when cutting/pasting the code because of the spaces. YAML is really really picky about that.

Input_number:

auto_lp_quartos_light_offset_on:
  name: Minutos Acerto ( - | + )
  icon: mdi:timer
  min: -90
  max: 90
  step: 10
auto_lp_quartos_light_offset_off:
  name: Minutos Acerto ( - | + )
  icon: mdi:timer
  min: -90
  max: 90
  step: 10

Sensor:

- platform: template
  sensors:
    auto_lp_quartos_light_group_state:
      friendly_name: 'Estado actual '
      icon_template: mdi:apple-keyboard-option
      value_template: '{{ states.group.lp_quartos.state }}'
    auto_lp_quartos_light_start:
      friendly_name: 'Luzes acendem às '
      icon_template: mdi:lightbulb-on-outline
      value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%H:%M") }}'
    auto_lp_quartos_light_stop:
      friendly_name: 'Luzes apagam às '
      icon_template: mdi:lightbulb-outline
      value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%H:%M") }}'
    auto_lp_quartos_light_minutes_start_offset:
      friendly_name: 'Minutos de Acerto '
      icon_template: mdi:timer-sand
      value_template: '{{ states("input_number.auto_lp_quartos_light_offset_on") | int }}'
    auto_lp_quartos_light_minutes_stop_offset:
      friendly_name: 'Minutos de Acerto '
      icon_template: mdi:timer-sand
      value_template: '{{ states("input_number.auto_lp_quartos_light_offset_off") | int }}'
    auto_lp_quartos_light_start_seconds:
      value_template: '{{ ((as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%H",0) | int * 3600 + (as_timestamp(states.sun.sun.attributes.next_setting) + (states("input_number.auto_lp_quartos_light_offset_on") | int) * 60) | timestamp_custom("%M") | int * 60)  }}'
    auto_lp_quartos_light_stop_seconds:
      value_template: '{{ (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%H",0) | int * 3600 + (as_timestamp(states.sun.sun.attributes.next_rising) + (states("input_number.auto_lp_quartos_light_offset_off") | int) * 60) | timestamp_custom("%M") | int * 60 }}'
    auto_lp_quartos_light_start_offset:
      friendly_name: 'Tempo até ligar (h) '
      icon_template: mdi:timelapse
      value_template: '{{ (((states.sensor.auto_lp_quartos_light_start_seconds.state | int)) - ((as_timestamp (now()) | int ))) | timestamp_custom("%H:%M") }}'
      entity_id:
        - sensor.time
    auto_lp_quartos_light_stop_offset:
      friendly_name: 'Tempo até desligar (h) '
      icon_template: mdi:timelapse
      value_template: '{{ (((states.sensor.auto_lp_quartos_light_stop_seconds.state | int)) - ((as_timestamp (now()) | int ))) | timestamp_custom("%H:%M") }}'
      entity_id:
        - sensor.time

Automation:

- alias: 'Acender Automático'
  initial_state: True
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 01
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.sensor.auto_lp_quartos_light_start_seconds.state | int <= ((((((as_timestamp (now(""))) | timestamp_custom("%H",0)) | int) * 3600) + ((((as_timestamp (now(""))) | timestamp_custom("%M")) | int) * 60) + (((as_timestamp (now(""))) | timestamp_custom("%S")) | int))) }}'
      - condition: state
        entity_id: group.familia
        state: 'home'
      - condition: state
        entity_id: 'group.lp_quartos'
        state: 'off'
  action:
    - service: light.turn_on
      data_template:
        entity_id: group.lp_quartos
    - service: notify.jorge_notify
      data:
        title: '{{ as_timestamp (now()) | timestamp_custom("%d/%b/%Y %T") }}'
        message: 'Luzes de Presença dos Quartos - LIGADAS'

- alias: 'Apagar Automático'
  initial_state: True
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 01
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: '{{ states.sensor.auto_lp_quartos_light_stop_seconds.state | int <= ((((((as_timestamp (now(""))) | timestamp_custom("%H",0)) | int) * 3600) + ((((as_timestamp (now(""))) | timestamp_custom("%M")) | int) * 60) + (((as_timestamp (now(""))) | timestamp_custom("%S")) | int))) <= states.sensor.auto_lp_quartos_light_start_seconds.state | int }}'
      - condition: state
        entity_id: 'group.lp_quartos'
        state: 'on'
  action:
    - service: light.turn_off
      entity_id: group.lp_quartos
    - service: notify.jorge_notify
      data:
        title: '{{ as_timestamp (now()) | timestamp_custom("%d/%b/%Y %T") }}'
        message: 'Luzes de Presença dos Quartos - DESLIGADAS'

Let me try to explain all that code.

Input_number:
auto_lp_quartos_light_offset_on - Minutes of offset to turn lights on
auto_lp_quartos_light_offset_off - Minutes of offset to turn lights off
(positive values means “after” sunset or sunrise, negative values mean “before” sunset or sunrise)

Sensor:
auto_lp_quartos_light_start - At what time the lights turn on (sunset time plus offset)
auto_lp_quartos_light_stop - At what time the lights turn off (sunrise time plus offset)
auto_lp_quartos_light_minutes_start_offset - Displays the start offset value in minutes
auto_lp_quartos_light_minutes_stop_offset - Displays the stop offset value in minutes
auto_lp_quartos_light_start_seconds - Converts the final turn on time to seconds
auto_lp_quartos_light_stop_seconds - Converts the final turn off time to seconds
auto_lp_quartos_light_start_offset - How long, in hours, until lights turn on
auto_lp_quartos_light_stop_offset - How long, in hours, until lights turn off
(this last two sensors gave me lots of headaches! It always gave an error or gave very strange numbers. In my case it only works if i have an entity_id: sensor.time inside the sensor… other people on the forum had the same problem and that was the solution they found too)

Automation:
Time trigger every minute and one second.
Condition checks if it is time to turn on or off the lights.
Action turns on or off the lights.

Are you still there??? Really?? Man, this is a loooooooooong post… Sorry about that. And sorry about my awful English, it’s not my first language as you might have guessed :blush:

If you have any question, just ask!

3 Likes

Mate, that’s amazing :smiley:

I’ll take some time to pick through all that and work it all out, thank you so much :+1:

Your English is perfect btw.

1 Like