Automation Trigger based on hour entity attribute value

Hello,

I’m trying to figure out a way to set up an automation to turn on / turn off a smart plug based on an entity that provide best energy prices (based on Hour and also price)… I’m open to best practices to configure this, but initially I was thinking to configure it based on Hour probably.

The entity I use is PVPC and the attributes I was aiming are “next_best_at” whih is a 24 Hour format, example: 19, 22, 23

So I think it would require a Float or some sort of date time conversion.
Someone have some example or idea to create this automation (specially the trigger) ? since it’s based on the value and the time

It’s hard to tell from what you have posted whether the attribute is a true list or just a listed-shaped string but one of he following should work…

trigger:
  - platform: template
    value_template: >-
      {{ now().hour in state_attr('sensor.pvpc', 'next_best_at') }}
trigger:
  - platform: template
    value_template: >-
      {{ now().hour in state_attr('sensor.pvpc', 'next_best_at').rsplit(",") 
      | map('int', 0) | list   }}
1 Like

Thanks for the reply!
Not sure how to tell you what kind of data is this attribute (if there is some way you can tell me so I can provide it to you).

Currently on the Attribute I see

next_best_at: 23, 17, 18, 22, 19, 21, 20

I’ve created the Automation as below but when I start it it stopped immediately

trigger:
  - platform: template
    value_template: "{{ now().hour in state_attr('sensor.pvpc', 'next_best_at') }}"
condition: []
action:
  - type: turn_on
    device_id: 88f50ac9c4156749b3fc672a0ca4c4bb
    entity_id: switch.tz3000_dpo1ysak_ts011f_switch
    domain: switch
mode: single

You don’t really need to know it… you can test the templates using the Template Editor tool. Just copy/paste each of the templates below in the tool. You may need to do one at a time, as the one that is wrong for your situation will likely just give an error message.

{{ now().hour in state_attr('sensor.pvpc', 'next_best_at') }}
{{ now().hour in state_attr('sensor.pvpc', 'next_best_at').rsplit(",") | map('int', 0) | list }}

Didn’t know this for testing. Just tested the following query

{{ now().hour in state_attr(‘sensor.pvpc’, ‘next_best_at’) }}

And the output is:

Result type: boolean
true
This template updates at the start of each minute.
This template listens for the following state changed events:
* **Entity**: sensor.pvpc

What I wonder is, Is this checking all the time once I start the Automation? Since when I start the Automation it stop, so not sure is if checking or not every hour

It checks the template at the start of each minute, but triggers require something to happen. The trigger will fire if the time change caused the template to render ‘true’ only when it was previously ‘false’. When you save or reload an automation and the template already equals ‘true’, the actions will not be executed.

If you need that functionality it can be added by including additional triggers and matching conditions.

trigger:
  - platform: template
    value_template: "{{ now().hour in state_attr('sensor.pvpc', 'next_best_at') }}"
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
condition: 
  - condition: template
    value_template: "{{ now().hour in state_attr('sensor.pvpc', 'next_best_at') }}"
action:
  - type: turn_on
    device_id: 88f50ac9c4156749b3fc672a0ca4c4bb
    entity_id: switch.tz3000_dpo1ysak_ts011f_switch
    domain: switch
mode: single