How to change/set an entity state?

Hi there! I’m rather new to HomeAssistant, and I find it quite hard to get into it, the learning curve seems rather steep.

I’ve written some simple code to turn on my Shelly1 (which turns on the water heater; note: I have the shelly app turn off Shelly after 55 minutes) at the 2 cheapest hours:

  - sensors:
        cheap_hour:
            friendly_name: "Cheap hour?"
            unique_id: chp_hrs_state
            value_template: >-
                {% set today=states.sensor.nordpool.attributes.raw_today|sort(attribute='value')%}
                {% set current_price = states('sensor.nordpool')|float %}
                {% if ( current_price <= today[1].value or current_price <= 0 ) %}
                    yes
                {% else %}
                    no
                {% endif %}

And added this in Automations:

alias: TurnOnShelly
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cheap_hour
    to: "yes"
condition: []
action:
  - type: turn_on
    device_id: 726474dd131501f8fd1aea80cb1ed2bd
    entity_id: 5de6ab698722f9611f11417540a662f9
    domain: switch
mode: single

This works well for the most part, except when the the 2 cheapest hours are X and X+1, because then the state doesn’t change, but simply stays yes, and the automation only turns Shelly on when the state changes to yes.

So a simple solution would be to change its state to ‘no’ every hour. I tried the following, but to no avail:

alias: SetState
description: ""
trigger:
  - platform: time_pattern
    minutes: "55"
condition: []
action:
  - platform: state
    entity_id: sensor.cheap_hour
    state: no2
mode: single

But this doesn’t seem to work (the editor gives an error: ““Message malformed: Unable to determine action @ data[‘action’][0]””). I feel like I don’t quite know what I’m doing, and ever topic that’s slightly relevant hasn’t offered a solution. Any help would be very appreciated!

That is a trigger, not an action. There are no services to change the state of an entity, that is left to its integration.

Get rid of that… Just set a second trigger for sensor.cheap_hour turning to “no”.

alias: TurnOnShelly
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.cheap_hour
    to: 
      - "yes"
      - "no"
condition: []
action:
  - if:
      - condition: state
        entity_id: sensor.cheap_hour
        state: "yes"
    then:
      - type: turn_on
        device_id: 726474dd131501f8fd1aea80cb1ed2bd
        entity_id: 5de6ab698722f9611f11417540a662f9
        domain: switch
    else:
      - type: turn_off
        device_id: 726474dd131501f8fd1aea80cb1ed2bd
        entity_id: 5de6ab698722f9611f11417540a662f9
        domain: switch
mode: single

That seems to be what I’m looking for, and is more elegant than what I had in mind. This night is another one which 2 consecutive cheapest hours, so I’ll check in the morning if it this did the trick. Thanks!!!

It works! (Not that I had any doubts, of course :wink:)

This also solves the bug where occasionally at 00:00 CheapHour would change to yes and then immediately to no. Before, Shelly would turn on for 55 mins, now it also immediately turns off.

Thanks again!

1 Like