Automation to turn on/off hotwater heater

Hi,
I am trying to automate the turning on of my hotwater system when the solar system output is above a certain kilowatts.
Currently I do this by monitoring the mqtt messages from the inverter, and then if time is greater than 10:00 and less than 14:00 and watts output is above 2800 watts it will turn on the hotwater heater. If the solar out put drops below 2800W it turns it off.

However the problem with this is I can not overide it or easily change the on/off times and kW threshold.

So I want to do something like in the pic.

Between the time set with the input_datetime on/off, and if the solar output is above whats is set with input_number slider, I want to turn on or off the hotwater. Or be able to over ride it by pushing the button on or off.

Below is what I have so far in my configuration.yaml file, which seems to turn on sometimes, but not off automatically. Manual over ride seems to work with button.

input_datetime:
  hotwater_off:
    has_time: true
    initial: '14:00'
  hotwater_on:
    has_time: true
    initial: '10:30'

input_number:
  hotwater_kilowatts:
    name: "Min Solar kW"
    mode: slider
    initial: '2.8'
    min: '2.6'
    max: '4.8'
    step: '0.2'

automation:
  - alias: "Set Hotwater kW slider"
    trigger:
      platform: mqtt
      topic: "hotwater_set_kw"
    action:
      service: input_number.set_value
      target:
        entity_id: input_number.hotwater_kilowatts
      data:
        value: "{{ trigger.payload }}"

  - alias: "hotawater kW slider moved"
    trigger:
      platform: state
      entity_id: input_number.hotwater_kilowatts
    action:
      service: mqtt.publish
      data:
        topic: "hotwater_set_kw"
        retain: true
        payload: "{{ states('input_number.hotwater_kilowatts') | float(1) }}"


  - alias: hotwater_on
    trigger:
      - platform: template
        value_template: "{{ (states.sensor.solar_now.state | int) > ((states.input_number.hotwater_kilowatts.state | float * 1000) | int )  and states.sensor.time.state > (states.input_datetime.hotwater_on.attributes.timestamp | int | timestamp_custom('%H:%M', False))}}"
    action:
      - service: switch.turn_on
        entity_id: switch.athom_3FEDCB

  - alias: hotwater_off
    trigger:
      - platform: template
        value_template: "{{ (states.sensor.solar_now.state | int) < ((states.input_number.hotwater_kilowatts.state | float * 1000) | int ) or states.sensor.time.state > (states.input_datetime.hotwater_off.attributes.timestamp | int | timestamp_custom('%H:%M', False))}}"
    action:
      - service: switch.turn_off
        entity_id: switch.athom_3FEDCB


switch:
  - platform: mqtt
    name: "athom_3FEDCB"
    command_topic: "cmnd/tasmota_3FEDCB/POWER"
    state_topic: "stat/tasmota_3FEDCB/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: false

Any help would be great, and is there a better/simpler way to achieve what I want to do ?

EDIT: Maybe it is working, after rebooting HA and then turning the button on/off it did seems to work. Maybe. Sort of. Anyways if anyone has any suggestings, or how it can be done better please let me know,

thanks

Taras - thank you for the help and examples - your less is more code i like.

So the mqtt number works great.
However the automation doesnt.
I put the old automation code back in and it works with the mqtt numbers.

If i change to below it works with your code:

  - alias: hotwater_on
    trigger:
      - platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) > (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) and
             now() > today_at(states('input_datetime.hotwater_on')) }}
    action:
      - service: switch.turn_on
        entity_id: switch.athom_3FEDCB

  - alias: hotwater_off
    trigger:
      - platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) < (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) or
             now() > today_at(states('input_datetime.hotwater_off')) }}
    action:
      - service: switch.turn_off
        entity_id: switch.athom_3FEDCB

I did notice with your automation code that both on and off slider switches act together, if I slide the on time switch the off time switch slides too.

Should this be the case ?

I have connected to tasmota power switch to a lamp so I can see whats happening and for testing before changing to the one on the hotwater cylinder.

For anyone that is interested in why I am doing this: Up until last July I had my hotwater heater/cylinder was on a timer, turning on at 10am and off at 2pm when solar output was around 5kW on a sunny day. However when it was raining or overcast the solar output was not enough to cover hotwater heating so I was sucking power from the grid. It might be overcast in the morning when the element would heat to heat, but sunny in afternoon.
Around 60% of my power usage is for hotwater. I had a 3.6kW element in the cylinder which was very fast (around an hour to 1 1/2 hours) to heat up 280lt of water, but also if I had other appliances running or was overcast/rain would be using more power than coming from solar.

So I changed out the element for a 2.4kW one, and then put in a mqtt controlled switch to turn on/off hotwater cylinder. I then monitor the solar inverter, when production is over 2.8kW I turn on the hotwater cylinder, and when it drops below that level it turns off. It might go for 2 or 3 days before the cylinder is turned on again depending on weather. I also turned off the cylinder to see how long the hot water would last before showers were no longer hot, which was 5 or 6 days.

Anyways as a result of the above simple changes which cost around AU$50, I have not paid the power company any money since July 2021, even with our pathetic grid feed in tariffs of AU$0.6583 per kWh and service charges, where prior to this I was paying $20 to $30 per month.
In the last year worst solar production for a month was 546kWh, best was 857kWh, average around 680kWh, so any credits I get from good months pay for the small charges ($3.50 highest so far) in the bad months.

Hi, the only thing i changed was to split it out like the code i posted above

Splitting it out doesnt work either… I edited the backup copy of my configurature.yaml file.

So all the automation part doesnt work.

My original automation works:
Your automation didnt work.

So I have tried the below which doesnt work:

  - alias: "Hotwater Controller"
    trigger:
      - id: 'on'
        platform: template
        value_template: "{{ (states.sensor.solar_now.state | int) > ((states.input_number.hotwater_kilowatts.state | float * 1000 ) | int ) and states.sensor.time.state > (states.input_datetime.hotwater_on.attributes.timestamp | int | timestamp_custom('%H:%M', False))}}"
      - id: 'off'
        platform: template
        value_template:  "{{ (states.sensor.solar_now.state | int) < ((states.input_number.hotwater_kilowatts.state | float * 1000 ) | int ) or states.sensor.time.state > (states.input_datetime.hotwater_off.attributes.timestamp | int | timestamp_custom('%H:%M', False))}}"
    action:
      - service: 'switch.turn_{{ trigger.id }}'
        target:
          entity_id: switch.athom_3FEDCB

And this doesnt work:

  - alias: hotwater_on
    trigger:
      - platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) > (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) and
             now() > today_at(states('input_datetime.hotwater_on')) }}
    action:
      - service: switch.turn_on
        entity_id: switch.athom_3FEDCB

  - alias: hotwater_off
    trigger:
      - platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) < (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) or
             now() > today_at(states('input_datetime.hotwater_off')) }}
    action:
      - service: switch.turn_off
        entity_id: switch.athom_3FEDCB

I made a backup of my configuration.yaml file (configurature.yaml.bak, and I edited that with the split version instead of the original configurature.yaml.

The configurature.yaml has in it now your number mqtt and my automation

UndefinedError: ‘today_at’ is undefined

Yes, Home Assistant 2021.7.4
I am waiting for a SSD to arrive so I can install a new one while old version is up.

As a record the below is what taras suggested I do - for some reason he/she/them or someone else deleted all taras’s posts.

Taras suggest to replace the following:

  • alias: “Set Hotwater kW slider”
  • alias: “hotawater kW slider moved”

with:

number:
  - platform: mqtt
    name: hotwater_kilowatts
    command_topic: hotwater_set_kw
    state_topic: hotwater_set_kw
    retain: true
    min: 2.6
    max: 4.8
    step: 0.2

And then make the automation as:

automation:
  - alias: "Hotwater Controller"
    trigger:
      - id: 'on'
        platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) > (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) and
             now() > today_at(states('input_datetime.hotwater_on')) }}
      - id: 'off'
        platform: template
        value_template: >
          {{ states('sensor.solar_now') | int(0) < (states('number.hotwater_kilowatts') | float(0) * 1000) | int(0) or
             now() > today_at(states('input_datetime.hotwater_off')) }}
    action:
      - service: 'switch.turn_{{ trigger.id }}'
        target:
          entity_id: switch.athom_3FEDCB

The automation part didn’t work - and there was no error when doing the CHECK CONFIGURATION in server control, however there was an error in developer tools/template for:
{{ now() > today_at(states(‘input_datetime.hotwater_off’)) }} , and tara made the comment I was using old version of home assistant, my version of home assistant is from july last year.
I haven’t done a homeassistent update as the last time I did one I lost all my configuration and had to start again, so am going to do a new install on a new SSD and then when it is working as I want will swap it to live system.