Automation based on binary_sensor, sensor or value template

Hi All

Really stuck here and pulling my hair that now think I need to start from scratch as everything I have tried does not seem to work

So, I currently use HACS and Octoblock to get the best 1hour time slot for my electricty which produces a sensor with the following:
sensor name: sensor.best1hourslot
state: 2020-10-11T14:30:00 BST

I want to automate my dishwasher to come on at the time of this sensor which dynamically changes. The dishwsher is on a smart plug and the automation works fine when I trigger it manually

I have tried setting up a binary_sensor, a sensor and a trigger in the automation but will not trigger

The sensor i created have the following as the value_template

- platform: template
  sensors:
    best1hourslot:
      friendly_name: Best 1Hour Slot
      value_template: >
        {% if (states("sensor.date_time_iso") + "Z") == (states("sensor.octoblock_1hour_time")) %}
          true
        {% else %}
          false
        {% endif %}

The binary_sensor produces a sensor with an on/off state
The sensor produces a sensor with an true/false state

If I manualy change the sensor to on or true the automation fires so, again my automation works, just not the template to update the sensor

Neither seem to change when i use as the triger monitoring the state from either off to on or false to true

Setting the trigger as the above template also does not fire that action/automation

Any suggestions?

you really havenā€™t given us enough info to go on.

What is the state of the ā€œsensor.octoblock_1hour_timeā€?

Also you should probably post the automation too for good measure.

state: 2020-10-11T14:30:00 BST

Will paste the automation in the morning

then Iā€™m not sure what you are trying to do here:

(states("sensor.date_time_iso") + "Z")

unless Iā€™m missing something then Iā€™m not sure how

2020-10-11T14:30:00Z

could be equal to

2020-10-11T14:30:00 BST

@finity

Thanks, You pointed me in the right direction and looked closer at the value of each sensor, and yep they would never match :frowning:

The original value_template was an example so started from scratch, played around with the individual sensor states and came up with the below states

states(ā€œsensor.date_time_isoā€)
2020-10-13T17:45:00

states(ā€œsensor.octoblock_1hour_timeā€))[:-4])
2020-10-13T18:00:00

I set the state of sensor.date_time_iso to 2020-10-13T17:59:00, waited till it ticked over and my automation fired

Testing seemed to have worked so now for the real automataion to kick in

Below is the final value_template I came up with

       {% if states("sensor.date_time_iso") == (states("sensor.octoblock_1hour_time")[:-4]) %}
         true
       {% else %}
         false
       {% endif %}
1 Like

Iā€™m wanting to do something similar but I also want to do something else after a period of time. i.e. having used octoblock to work out that at 2200 there is a cheap 1hr , then I want to switch something on at that time for 1 hour. Iā€™m new to HA so Iā€™m not sure if the format of automations.yaml has changed as many of the code fragments I have seen in the forum throw an error so I am trying to do this via the automations wizard ( Configurations\Automations ) : I have a couple of entities
sensor.date_time_iso = 2020-11-12T13:55:00 and
sensor.octopus_1hour_time = 2020-11-12T22:00:00Z .
The automations.yaml currently looks like this :

 - id: '1605184438058'
  alias: Switch on Light for 1 hour
  description: ''
  trigger:
  - platform: template
    value_template: '{% if (states("sensor.date_time_iso") + "Z") == (states("sensor.octopus_1hour_time"))
      %}true{% endif %}'
  condition: []
  action:
  - service: switch.turn_on
    data: {}
    entity_id: switch.sonoff_xxxxxyyyyy
  mode: single

this was created with the wizard , can anyone advise how to modify this to include the switch_off after 1 hour piece ( ideally using the wizard since Iā€™ve had as much luck modifying the yaml code directly as buying a lottery ticket :slight_smile:)

add the following to your action section after your on action

  - delay: 00:60:00
  - service: switch.turn_off
    entity_id: switch.sonoff_xxxxxyyyyy

Or in thw wizard add an action of delay after you switch on action, then add the off action