Updating maximum using a helper `input_number` and an automation

I have an automation to track the historical maximum value of an entity, but it’s not working somehow, I can’t figure out the mistake I’m making, maybe someone can see it. I suspect the comparison trigger is failing, it never triggers somehow, but I do not see what’s wrong.

And if there’s a simpler way to do this, I’m all ears.

alias: Monitor Maximum Power consumption
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.smartmeterreader_current_power
    above: input_number.max_current_ever
condition: []
action:
  - service: number.set_value
    target:
      entity_id: input_number.max_current_ever
    data:
      value: >-
        {% set current_value = states('sensor.your_sensor_name') | float %} {%
        set max_value = states('input_number.maximum_sensor_value') | float %}
        {{ [current_value, max_value] | max }}
mode: single

and this is input_number.max_current_ever:

initial: 0
editable: true
min: 0
max: 100000
step: 1
mode: slider
unit_of_measurement: W
icon: mdi:home-lightning-bolt
friendly_name: Max Current ever

Should be input_number.set_value, and the template in the action references “your_sensor_name” and “maximum_sensor_value” — have you just copy/pasted those from someone else’s code?

Here’s my very similar automation:

- alias: Energy - max power recorder
  description: Updates the maximum power recorder whenever prior maximum is exceeded

  trigger:
    - platform: numeric_state
      entity_id: sensor.power_meter_house
      above: input_number.max_power
  
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.max_power
        value: "{{ states('sensor.power_meter_house')|float(0) }}"

You don’t need the detail in the action template: if you’ve got there, the sensor is higher than the input_number.

oops, I pasted the wrong code in here, sorry. But I did have the error with the wrong service in HA though, good catch, thanks. But it does not help, it still does trigger at all, there is nothing under “traces”.

I simplified the automation for troubleshooting purposes and attempting to simply set the number to 10. But even that is not happening:

alias: Monitor Maximum Power consumption
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.smartmeterreader_current_power
    above: input_number.max_current_ever
condition: []
action:
  - service: input_number.set_value
    metadata: {}
    data:
      value: 10
    target:
      entity_id: input_number.max_current_ever
mode: single

but I see under developer > states, that the value of input_number.max_current_ever is 0 while the value of sensor.smartmeterreader_current_power is oscillating above 200, so how could it not trigger?..

I feel like I’m missing something obvious. As if sensor.smartmeterreader_current_power and input_number.max_current_ever cannot be compared.

A numeric_state trigger will only fire when the value crosses the threshold. If the input_number is set to 0 that will never happen. Set it manually to somewhere in the middle of the range of numbers that the smart meter is sending and it should start to work.

alias: Monitor Maximum Power consumption
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.smartmeterreader_current_power
    not_to:
      - unavailable
      - unknown
    variables:
      new: "{{ trigger.to_state.state | float }}"
condition:
  - condition: template
    value_template: |
      {{ new > states('input_number.max_current_ever') | float }}
action:
  - service: input_number.set_value
    metadata: {}
    data:
      value: "{{ new }}"
    target:
      entity_id: input_number.max_current_ever
mode: queued

Thanks!

I kept the below code for the data because my sensor is very chatty and I am worred the value will change while the automation is executing, resulting is the input_number.max_current_ever being overwritten with a lower value.

{% set current_value = states('sensor.your_sensor_name') | float %}
{% set max_value = states('input_number.maximum_sensor_value') | float %}
{{ [current_value, max_value] | max }}

Again, the template you have posted uses input_number.maximum_sensor_value but your entity is input_number.max_current_ever.

Check that the sensor and input_number entity IDs in the template match the ones in your system.

Gosh, I keep doing this. I copied from the top of the thread.

This time I copied from my HA, it should be correct:

alias: Monitor Maximum Power consumption
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.smartmeterreader_current_power
    above: input_number.max_current_ever
condition: []
action:
  - service: input_number.set_value
    metadata: {}
    data:
      value: >-
        {% set current_value = states('sensor.smartmeterreader_current_power') |
        float %} {% set max_value = states('input_number.max_current_ever') |
        float %} {{ [current_value, max_value] | max }}
    target:
      entity_id: input_number.max_current_ever
mode: single

I set initial value of input_number.max_current_ever slightly above the current consumption, not too high but rather a value I knew would be soon crossed, and so far it works.

1 Like