Automation that enables a device when a button was pressed in the last 12 hours

Hi all,

A bit of a newbie here, although I’ve been playing around with stuff for a few months now. I’m trying to create an automation that switches the socket of the car charger to on when we’re getting cheap power (either due to time based tariffs or due to our solar panels sending most of the energy back onto the grid). The code I have is the following:

alias: Charge car
description: "Charge the car only when it is cheap for us"
trigger:
  - platform: time_pattern
    minutes: /15
condition: []
action:
  - if:
      - condition: or
        conditions:
          - type: is_power
            condition: device
            device_id: de55a9e4519fcabc29348768ebb78143
            entity_id: 64320c8b65628662bbd5b0e5a66e0da2
            domain: sensor
            above: 1500
          - condition: state
            entity_id: sensor.p1_meter_3c39e7252b62_active_tariff
            state: "1"
          - condition: and
            conditions:
              - type: is_power
                condition: device
                device_id: b5a91b0169b98067880cf0335555df9e
                entity_id: 35ca72049528c6c9e5bc5bd6e1551dfe
                domain: sensor
                above: 2000
              - type: is_power
                condition: device
                device_id: de55a9e4519fcabc29348768ebb78143
                entity_id: 64320c8b65628662bbd5b0e5a66e0da2
                domain: sensor
                below: 500
    then:
      - type: turn_on
        device_id: b5a91b0169b98067880cf0335555df9e
        entity_id: 593bca0270120e853e9290bdb5a00782
        domain: switch
    else:
      - type: turn_off
        device_id: b5a91b0169b98067880cf0335555df9e
        entity_id: 593bca0270120e853e9290bdb5a00782
        domain: switch
mode: single

I’d like to make two improvements, but having trouble to make it work, hopefully someone can help me:

  • I added an Input button that would be pressed when we want to charge the car no matter of the costs. Basically, I’d like to check if the button was pressed within the last 12 hours, but I have no idea where to start that condition.

  • Currently, I’m checking the actual latest power usage ever 15 minutes, but it would probably make sense to check the average usage over the last 15 minutes. I’m guessing I should make a Helper for that? Should I create a Helper for each of the measurements I’m using (P1 Power, Charger Socket Power) or is there some generic thing I should be using instead?

Thanks for any guidance you can provide! Also, is there some sort of tutorial that does some useful stuff from the get-go with HA scripting/automating that guides a newbie through some of the basics?

If I’ve understood you correctly, you want to turn the socket on when power’s cheap - so that’s your trigger, you don’t need to check every 15 minutes.

trigger:
  - platform: numeric_state
    entity_id:
      - sensor.your_solar_panel_output
    above: 1500
  - platform: state
    entity_id:
      - sensor.p1_meter_3c39e7252b62_active_tariff
    to: "1"

You can have several triggers and the automation will run when any one of them is true, so the input button would just be an extra one.

You’ll find things a lot easier if you don’t use device ids.

Have you followed through the introductory pages of the docs?

1 Like

If I’ve understood you correctly, you want to turn the socket on when power’s cheap - so that’s your trigger, you don’t need to check every 15 minutes.

That’s indeed what I want to do, but I’m using them as conditions instead of triggers to be able to use the if-then-else, I don’t know how else to do the else.

You’ll find things a lot easier if you don’t use device ids.

Tbh, I’m using the visual editor for most of it, as I find it very convenient to search for the correct devices/entities that way and then just edit the yaml and copy/paste stuff around as I see fit.

Have you followed through the introductory pages of the docs?

Yes, but it’s very, very basic. I usually just google stuff and end up on this forum where someone asked something similar. But I’ve yet been unable to find how I can do the “check if the button was pressed in the last 12h”. Or maybe I should use a timer instead. I just have no idea what makes the most sense in this case :confused:

Thanks for your guidance, in any case!

Input buttons don’t have states like other entities, but there is a timestamp showing when they were last pressed which you can use as a trigger.

Simplest approach might be to create a separate short automation to turn an input boolean on when the button is pressed. The input boolean would retain an on/off state which you could use.

trigger:
  - platform: state
    entity_id:
      - input_boolean.from_your_button
    to: "on"
    for:
      hours: 12
      minutes: 0
      seconds: 0

or…

condition: state
entity_id: input_boolean.from_your_button
state: "on"
for:
  hours: 12
  minutes: 0
  seconds: 0

Depending on how you were going to approach it.