How to set the numeric state of an entity

Hi,
I feel dumb, because I have been searching how to set the (numeric) state value of an entity using an automation action

Even CoPilot can’t help me out. It must be rather simple but I dont find the service which can do this.

After 45 minutes of trying I need help of you guys. :grimacing:

Thanks in advance.

What type of entity?

If they exist, the automation actions that are available to act on an entity will be found in their integrations’ docs under the Actions section.

For example: Input Number - Actions

Maybe it is best to describe what you want to achieve rather than “setting an entity” which cannot be the ultimate goal. Most numerical entities are sensors, they cannot be set by users (think of room temperature, etc).

Plus there might be alltogether other approaches to what you want to achieve.

Okay fair enough

Here’s my problem

I have a solarpanel integration (Growatt). One of the entities hold the power in watt of the current power generation. When the sun goes down this value should be 0 eventually. But for some reason the value stays between 5 and 8 watt during the night and rises again when sun rises. So the inverter does not sent the correct value when it’s low.

So I wanted to create an automation which triggers when the value of the power generation is below 10 and then set the value to 0.

Is this clear enough?

I don’t use the Growatt integration, or any solar integration for that matter, so take the below advice with a pinch of salt:

I think you’re going about this the wrong way and need to verify whether the integration is reporting the correct wattage all day long, not just when it’s night.

Assuming your panels aren’t actually picking up some stray ambient light and actually producing power thanks to some neighbour’s floodlights, have you verified with a multimeter that what is reported does not match what is actually being produced?

I’m asking this because it could simply be a matter of calibrating your integration or meter. If neither of these options is possible and your integration reports elevated wattage also during the day, then you’ll probably need to create a template to knock down the reported wattage by 5-8w all the time, not just at night.

If this only happens at night, then again, a template sensor should be able to handle this. Unfortunately I suck at templates, so hopefully someone else with more experience will step up to show you how it’s done.

Either way, you can’t use an automation to override a sensor value. At best, it’ll have to trigger constantly on every report until the value is greater than 10w

Yes. The solution would not be to “set” the original sensor, but create a sensor to have the right behavior. That would be a template sensor. The template would copy the value from the inverter when it is ok.
How do you plan to determine when the value should go to 0?

Something like this:

template:
  - sensor:
      - name: Adjusted Solar Power
        device_class: power
        state_class: measurement
        unit_of_measurement: W
        state: >
          {% if is_state('sun.sun','above_horizon') %}
            {{ states('sensor.solar_power') }}
          {% else %}
            0
          {% endif %}
        availability: "{{ states('sensor.solar_power')|has_value }}"

You can change the conditional test {% if is_state('sun.sun','above_horizon') %} to anything you want.

2 Likes

Hi @tom_l

It seems the state is not numeric

Logger: homeassistant.components.sensor
Bron: helpers/entity_platform.py:676
integratie: Sensor (documentatie, problemen)
Eerst voorgekomen: 14:36:20 (1 gebeurtenis)
Laatst gelogd: 14:36:20

Error adding entity sensor.adjusted_solar_power_oost for domain sensor with platform template
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 689, in state
    numerical_value = float(value)  # type:ignore[arg-type]
ValueError: could not convert string to float: "states('sensor.dng0a4702r_pv2_power')"

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 676, in _async_add_entities
    await self._async_add_entity(
        entity, False, entity_registry, config_subentry_id
    )
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 1002, in _async_add_entity
    await entity.add_to_platform_finish()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1380, in add_to_platform_finish
    self.async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1024, in async_write_ha_state
    self._async_write_ha_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1149, in _async_write_ha_state
    self.__async_calculate_state()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1086, in __async_calculate_state
    state = self._stringify_state(available)
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1030, in _stringify_state
    if (state := self.state) is None:
                 ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sensor/__init__.py", line 691, in state
    raise ValueError(
    ...<5 lines>...
    ) from err
ValueError: Sensor sensor.adjusted_solar_power_oost has device class 'power', state class 'measurement' unit 'W' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-numeric value: 'states('sensor.dng0a4702r_pv2_power')' (<class 'str'>)

Share your config.

Hi @tom_l

This is the template based on your example

sensor:
  - name: Adjusted Solar Power Oost
    default_entity_id: sensor.adjusted_solar_power_oost
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    state: >
      {% if is_state('sun.sun','above_horizon') %}
        {{ states('sensor.dng0a4702r_pv2_power') }}
      {% else %}
        0
      {% endif %}

And this is a screenshot of the ‘source’ sensor

Is this enough?
I am running the latest versions of HA

Thanks in advance,
Rien

Not quite. You are missing a - in front of sensor:

- sensor:
    - name: Adjusted Solar Power Oost
      device_class: power
      state_class: measurement
      unit_of_measurement: W
      state: >
        {% if is_state('sun.sun','above_horizon') %}
          {{ states('sensor.dng0a4702r_pv2_power') }}
        {% else %}
          0
        {% endif %}

And you dont need the default entity id. It will be exactly the same as the entity id auto generated from the name.

It is part of my template.yaml so I don’t need the -

That is not true.

You template.yaml has to contain a list of entities.

Strange because all my other templates in the templates.yaml do work