Automation fails to writhe input_number float issue

To calculate my daily electricity consumption I created an input_number. An automation at 00:00 reads my DSMR meter and writes the value in the input_number. A template calculates the ‘real-time’ consumption by subtracting the value read at 00:00 (input_number value) from the actual value.

When I run the automation manually it works fine:


At night at 00:00:01, when it’s sheduled it gives an error:

The error: expected float for dictionary value @ data[‘value’].

In the details below can be seen that the input_number is a float.

input_number.yaml:

teller_nacht_dagtarief:
  name: Teller nacht dagtarief
  min: 0
  max: 999999999

automation.yaml:

id: Verbruik Huis / leg elec dagverbruik totaal teller vast om 00:00
alias: Verbruik huis / leg ELEC DAG vast
trigger:
  platform: time
  at: '00:00:01'
action:
  service: input_number.set_value
  data_template:
    entity_id: input_number.teller_nacht_dagtarief
    value: '{{ states.sensor.energy_delivered_tariff1.state }}'

When I run the automation manually the input_number has an updated State:


from the sensor energy_delivered_tariff1:

(There is a litte difference because I took the printscreens with a delay)
At midnight (00:00:01) nothing happens because the automation fails.

Not related to the question but probably helpfull if someone wants to copy/paste this scenario, finally the template to calculate:

template.yaml:

      - name: dagverbruik_elec_dag
        unique_id: dagverbruik_elec_dag
        state: "{{ (states('sensor.energy_delivered_tariff1')|float(0) - states('input_number.teller_nacht_dagtarief')|float(0)) | round(2) }}"
        unit_of_measurement: "kWh"

Nope.

States are always strings. The template editor interprets these results but templates dont do this. See https://github.com/home-assistant/core/issues/46198

Attributes can be other types than strings. But states are always strings.

You are also using a method to extract the state that can result in errors.

You need to do this:

action:
  service: input_number.set_value
  data_template:
    entity_id: input_number.teller_nacht_dagtarief
    value: "{{ states('sensor.energy_delivered_tariff1')|float(0) }}"

1 Like

Thanks @tom_l

Still curious why the automation works when manually fired and fails at night (automatically triggered by time).

I changed the automation:

- id: Verbruik Huis / leg elec dagverbruik totaal teller vast
  alias: Verbruik huis / leg ELEC DAG vast
  trigger:
    platform: time
    at: 00:00:00
  action:
    service: input_number.set_value
    data_template:
      entity_id: input_number.teller_nacht_dagtarief
      value: "{{ states('sensor.energy_delivered_tariff1')|float(0) }}"

Checked configuration: passed without errors. Reloaded automations and got no errors.
Noticed that the automation is greyscaled in the Automations list and has the state ‘unavailable’.
Even restarted HA completely but remains greyscaled. Believe the supplied code is not fully correct.

Other strange thing, but maybe related to above issue; when I change the trigger time to test it now, let’s say 5 minutes from the actual time, for example 11:40:00 and I reload my automation I get below error:

Logger: homeassistant.config
Source: config.py:464
First occurred: 11:31:24 AM (1 occurrences)
Last logged: 11:31:24 AM

Invalid config for [automation]: Expected HH:MM, HH:MM:SS or Entity ID with domain 'input_datetime' or 'sensor' @ data['at'][0]. Got None. (See /config/configuration.yaml, line 10).

The time format HH:MM:SS seems to be correct…

Quote the time.

  trigger:
    platform: time
    at: '00:00:00'

Unquoted colons (:) do funny things to the interpretation of yaml. They are the separator between the key and the value.

Automation runs triggered by time and input_number is updated.
Many thanks @tom_l !

1 Like