Automate with a Numeric Condition based on a history_stats sensor

I am new to Home Assistant, and YAML etc, but slowing getting there and using it to do some sensible things. But I am hoping someone can help me solve a problem.

I am trying to automate the pool pump to try and get it to primarily run from spare Solar. But when there is not enough, I am happy for it to keep running.

I have an automation, which switches the pump off once I start needing to import power from the grid which is all working. However, I would like to add a condition which says to leave it on, if the pump has not run at least 4 hours today AND 8 hours in the last 2 days. So I have setup a history_stats sensor which tracks how many hours the pump has run today, as well as in the last to days. And I added a condition that makes sure that these sensors have run for 4 and 8 hours respectively. HOWEVER, this does not work, because the history_stats sensor does not seem to like a number (I assume because it is a time). The error is :-

Error: In 'and' (item 1 of 2): In 'numeric_state': In 'numeric_state' condition: entity sensor.pool_pump_on_today state '' cannot be processed as a number

To give you an idea, my not working code is below :-

- id: '1663655246419'
 alias: Pool - Turn off if grid import and it has run 8 hours in 2 days AND 4 hours
   today
 description: ''
 trigger:
 - platform: numeric_state
   entity_id: sensor.modbus_grid_power_sum
   above: 0
   for:
     hours: 0
     minutes: 0
     seconds: 0
 condition:
 - condition: and
   conditions:
   - condition: numeric_state
     entity_id: sensor.pool_pump_on_today
     value_template: ''
     above: 4
   - condition: numeric_state
     entity_id: sensor.pool_pump_on_last_2_days
     above: 8
   enabled: true
 action:
 - type: turn_off
   device_id: 6bbc4ecb610c42d40ac51b9217de14bb
   entity_id: switch.wifi_plug_3_socket_1
   domain: switch
 mode: single

Clearly a “numeric state” condition does not like what is in history_stats because it is a time.

Can anyone help me with an alternate way to implement this which would achieve what I want to achieve?

I should mention, I have another automation which picks up the pieces, and will turn it off if grid imports is above zero for 30 minutes. But I very much want to have it turn off earlier if we have achieved the runtime target.

Thanks in advance.

Waht is the state of sensor.pool_pump_on_today?

Look in Developer Tools → States to get the exact state.

Thanks Tom for the quick reply.

State is 6.9. But the unit of measurement is h:, as you can see here :-

state_class: measurement
unit_of_measurement: h
device_class: duration
icon: mdi:chart-line
friendly_name: Pool Pump ON today

I hope it would work as a number, but as you can see from the error in the trace I did, it does not seem to be. I am hoping there is a quick fix/workaround I can do?

It definitely should.

Try this in the template editor:

{{ states('sensor.pool_pump_on_today')|is_number }}

What do you get, true or false?

EDIT: I see the issue.

Remove this:

     value_template: ''

It is setting the value to null.

Thanks. I created this in the GUI. Do you know how I remove this in the GUI, and just as importantly, how I created this in the GUI? Or is it some bug I need to remove in the CLI and be done with?

I don’t use the automation editor. Just delete the condition and recreate it. Your other numeric condition does not have a value_template.

Or view it in YAML and delete it.

Tom you are an absolute champion. That seems to have worked (remove from CLI).

It also explains why I thought it had worked before (no doubt before the value_template: ‘’ snuck in. This probably happened in the GUI, so I recreated what probably happened. I put something in there, and then deleted it and saved it. That is enough to put that like and and leave it there. I suspect it is a bit of a bug for GUI users which is hard to see and identify. I assume if that feild in nul, the like should be remove from the config, not leaving as value_template: ‘’.

But not sure if it is important enough to raise.