Control a device based on value of other/global variable

Hello Guys

Usually am able to find all in need in this forums, or i find my answers using google. However not this time and I believe I have very simple request on my hassio. I appreciate any help. So here is my situation:
Currently am using aqara smart power outlet to control my boiler. Am heating the water (turn on the power outlet) based on the time of the day - electricity tariff from my provider., currently manually. I would like to automate this process in a clever way. The tariff basically says:
00 00 - 8 30 → heating on (cheap power)
8 30 - 9 30 → heating off (expensive power)
9 30 - 10 30 → heatin on
etc.

Thing is that in a near future I plan to buy more smart outlets and control more devices based on the tariff, or even control my thermostat based on tariff. And i would be happy if I can control many automation based on ONE value, so i dont need to put many conditions manually one by one.
I was already doing little research and learned about Helpers, which can be found in Configuration. I guess in my case input boolean helper, which would change is state would be a first step.
But how can I tell the Helper to change its value as the time goes during the day ? And then how to tell to automation to turn off / on the device based on the Helper value?

Thank you for all answers
Rado

In both cases, you can use the Choose action.

First, use an automation to flip your input_boolean using a Choose action… you can give multiple triggers the same trigger_id and they will execute the same branch of the Choose action.

alias: Tariff times
description: 'Flip input_boolean.power_is_cheap at certain times'
mode: single
trigger:
  - platform: time
    at: '08:30:00'
    id: High Tariff
  - platform: time
    at: '09:00:00'
    id: Low Tariff
  - platform: time
    at: '10:30:00'
    id: High Tariff
  - platform: time
    at: '00:00:00'
    id: Low Tariff
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: High Tariff
        sequence:
          - service: input_boolean.turn_off
            target:
              entity_id: input_boolean.power_is_cheap
      - conditions:
          - condition: trigger
            id: Low Tariff
        sequence:
          - service: input_boolean.turn_on
            target:
              entity_id: input_boolean.power_is_cheap
    default: []

For the second part, you would set the state of your boolean as a trigger for your automation(s) then the choose could be based on a trigger variable template.

alias: Switch off Boiler (tariff-based)
description: ''
mode: single
trigger:
  - platform: state
    entity_id: input_boolean.power_is_cheap
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == "on"}}'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.boiler_switch
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == "off"}}'
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.boiler_switch
    default: []

This part can be compacted greatly by using templates… and can handle multiple switches all at the same time:

alias: Switch off Boiler (tariff-based)
description: ''
mode: single
trigger:
  - platform: state
    entity_id: input_boolean.power_is_cheap
condition: []
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: 
        - switch.smart_plug_1
        - switch.smart_plug_2
        - switch.boiler_switch

As stated by the poster above, I would do this with an input_boolean, on - cheap power, off - expensive power. Then simply create an automation that reacts to the state change, and use the choose in the actions part.

I do something similar but not for electric, I have an input_boolean for lighting_node_mode and another for house_in_night_mode.

If the boolean is on, then motion detectors will trigger the light in that room to switch on, but if the boolean is off - then they won’t. The house_in_night_mode one is for when we have gone to bed, it lowers the heating, and those same automations that trigger the lights, will trigger the lights at a much lower brightness, and the time they stay on for is much reduced to just a few minutes.

FWIW, instead of an automation that sets an input_boolean, you can create a Trigger-based Template (Binary) Sensor. It would use the same triggers as in your first automation but eliminate the need for an input_boolean (because it would serve as a binary_sensor).

Thank You all for answer

Especially thanks to @Didgeridrew ,I already have the solution in place and it works, easy and understandable. I was not aware that you can have multiple triggers with same name, thanks for that. Also, would take me some time to figure out the template syntax, even though is the most basic one i guess :slight_smile: never used it before

Also @123 Could you please elaborate, maybe provide a code example how to use the Trigger-based template? Am still kinda new in all hassio and dont really see the complete picture with that solution. I would be happy if I learn how to make things work even easier if possible

Which part of the linked example did you have trouble understanding?

How would i configure the template in yaml, so it toggles on/off in desired times?
And then, it the automation, for my case boiler on/off i would just use the same process as described in Didgeridrew answer, right?

That question implies you either have not read or understood the documentation I had suggested. Which one one was it, not reading or not understanding?

well i would not ask you if i understood it right?
I have read it, however as stated, am still a newbie in this area

So which part did you not understand? A Trigger-based Template Sensor has triggers, like an automation, so that’s where you would define Time Triggers like the ones used in Solution post.

In its most basic form, a template binary sensor to replace the first automation and the input_boolean would look like:

#configuration.yaml

template:
  - trigger:
      - platform: time
        at: "08:30:00"
        id: High Tariff
      - platform: time
        at: "09:00:00"
        id: Low Tariff
      - platform: time
        at: "10:30:00"
        id: High Tariff
      - platform: time
        at: "00:00:00"
        id: Low Tariff
    binary_sensor:
      - name: "Low Tariff"
        state: >-
          {{ trigger.id == "Low Tariff" }}

This would create an entity called sensor.low_tariff which would return “on” if it was triggered to update by one of the “Low Tariff” triggers and return “off” if it was triggered to update by one of the “High Tariff” triggers or if it couldn’t define what the trigger was (such as after a power outage or reboot).

There is a pretty good template basics video from Lewis at Everything Smart Home on Youtube. Be aware that he uses the older format for his template sensors because the video came out before June 2021. That format still works for basic template sensors, but it does not support triggers.

1 Like