Automation - Turn Heater On Every 2 Hours

Anyway, it should be working with minutes…

1 Like

yes i found that and followed that guide, however mine still doesnt work for some reason.
it was turning every hour instead of every 2 hours.

I tried with the HOURS, automation didnt fire at all. as of right now I just created 4 separate automation based on AT specific time, until i can figure out why.

cheers

Hi,
Did you find how to fix it ?
I would like to use the same solution.

Thank you in advance

@mateola, the way I read the code, even though the docs don’t say it, the time trigger does support seconds, minutes and hours. So you should be able to do (although I haven’t tried myself):

trigger:
  platform: time
  hours: '/2'
  minutes: 0
  seconds: 0

The reason this:

trigger:
  platform: time
  minutes: '/120'
  seconds: 0

doesn’t work (i.e., triggers every hour instead of every two hours) is because it does a modulo operation of the current minute with the value provided and triggers if the result is zero. But minute only goes from 0 to 59. So, every time the minute is 0 it will trigger (i.e., 0 % 120 == 0), which happens once every hour.

2 Likes

Pretty sure the issue is with your condition not your trigger. First, in your condition, you don’t have quotes around your before time. All times need quotes.

I also got rid of your ID. Usually that’s an integer. Not sure what a huge ass string will do in that. It’s also auto populated so you don’t need to add it yourself.

- alias: FT Bed Heater Auto On for 30 Minutes Every 2 Hour From 11pm to 6am
  trigger: 
    platform: time
    minutes: '/120'
    seconds: 0
  condition:
  - condition: time
    after: '23:00:00'
    before: '06:00:00'
  action:
  - entity_id: switch.ftheater
    service: homeassistant.turn_on
  - delay: 00:30:00
  - entity_id: switch.ftheater
    service: homeassistant.turn_off

Also, all the talk about the trigger may be correct. I’ve never made this trigger beyond 60 minutes.

EDIT: After reading @pnbruckner’s response, I tend to agree with him about the trigger occuring once an hour due to seconds going from 0 to 59.

It’s only auto-populated if the automation editor was used, and it can be any unique value.

1 Like

thanks for clearing that up.

The docs say that hour:, minute: and second: are supported, and that you can use / notation in any of them.

So,

  trigger: 
    platform: time
    hours: '/2'
    minutes: 0
    seconds: 0

Should work

So, I followed the code too and you are 100% correct, /120 won’t work because they are using mod (%) against time. So @mateola final automation should be:

- alias: FT Bed Heater Auto On for 30 Minutes Every 2 Hour From 11pm to 6am
  id: FT Bed Heater Auto On for 30 Minutes Every 2 Hour From 11pm to 6am
  trigger: 
    platform: time
    hour: '/2'
    minutes: 0
    seconds: 0
  condition:
  - condition: time
    after: '23:00:00'
    before: '06:00:00'
  action:
  - entity_id: switch.ftheater
    service: homeassistant.turn_on
  - delay: 00:30:00
  - entity_id: switch.ftheater
    service: homeassistant.turn_off
2 Likes

Perfetc !! Thank you guys

Using hour throws a config check error for me:

  trigger: 
    platform: time
    hour: '/2'
    minutes: 0
    seconds: 0

Invalid config for [automation]: [hour] is an invalid option for [automation]. Check: automation->trigger->0->hour.

However using minutes doesn’t work either (runs every hour regardless of value). Here’s my automation:

- id: speed_tests
  alias: 'Run speed test every 6 hours'
  trigger:
    platform: time
    minutes: '/360'
    seconds: 0
  condition:
    condition: state
    entity_id: sensor.steam_123
    state: 'offline'
  action:
    service: sensor.update_speedtest

It’s hours:, not hour:.

2 Likes

In case anyone is following this now, in later versions of Home Assistant the time platform has been split into time and time_pattern. See https://www.home-assistant.io/docs/automation/trigger/ .
In this case it would now be:

trigger:
  platform: time_pattern
  hours: xxxx
  minutes: xxxx
  seconds: xxxx
condition:
 ........
5 Likes

Thank you! It would have taken me forever to figure this out had I not seen this last comment.

the “/2” patern is clear - for every even hour.
what if I need every odd hour ?

add this condition for even:

- condition: template
  value_template: "{{ now().hour % 2 == 0 }}"

add this condition for odd:

- condition: template
  value_template: "{{ now().hour % 2 == 1 }}"

thanks Petro.
I’ve done exactly so as “workaround”, I just thought there is more clever solution.
anyway - thanks for quick response.

regards,
Valdas

It’s not a work around, it’s the current way unfortunately. Maybe in the future they will add a attribute to trigger that filters even and odd. I doubt it’s even on the dev’s radar. Might want to add a feature request.

Condition would be best way to do the odd hour thing. I don’t think any automation system, commercial or DIY, are granular enough on the triggers to split odd/even hours like that without using a condition.