Setting a value in an entity via yaml

Can someone please give me the yaml code for simply setting a value in a specific entity. I’m trying to set a default other than null at midnight.

What is the entity id?

It is one I made via template,
name: sensor.lightning_strike_history
state_class: measurement
unit_of_measurement: Miles
device_class: distance
friendly_name: Lightning strike history

Please share the full config correctly formatted for the forum by using the </> button in the post menu or by doing this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

sensor.lightning_strike_history
state_class: measurement
unit_of_measurement: Miles
device_class: distance
friendly_name: Lightning strike history

That is not a template sensor config.

  1. It has no state template.

  2. It is not indented correctly

  3. It is using an old depreciated formal.

How about telling us what you want the sensor to do instead. Including what the input entities are and how you expect the output to change when the input entities change.

- sensor:
  - default_entity_id: sensor.lightning_strike_history
    name: Lightning strike history
    state_class: measurement
    unit_of_measurement: Miles
    device_class: distance
    state: '{{states(''sensor.gw1100b_lightning_strike_distance'') }}'

The purpose of this is; the sensor.gw1100b_lightning_strike_distance sensor only privides the last stike distance. I am attempting to build a daily graph that will show the distance of each lightning strike during the day. I created this entity to keep a history of the strikes.

And it is losing it’s state after a while and you want to keep the last state?

If so:

- sensor:
  - default_entity_id: sensor.lightning_strike_history
    name: Lightning strike history
    state_class: measurement
    unit_of_measurement: mi
    device_class: distance
    state: >
      {% if has_value('sensor.gw1100b_lightning_strike_distance') %}
        {{ states('sensor.gw1100b_lightning_strike_distance') }}
      {% else %}
        {{ this.state }}
      {% endif %}

If you look closely you will notice I changed the unit to mi. You must use one of the units listed here for the device_class “distance”: Sensor - Home Assistant

If your gw1100b sensor has a value it will be used. If it does not have a value (unknown, unavailable, null) it will keep the last value (this.state).

It may error the first time you use it if the value is null and there was no previous state for the template sensor. You can fix this by setting the value of sensor.lightning_strike_history in Settings → Developer Tools → States to some default value to start ift off.

That’s not the issue. The sensor.gw1100b_lightning_strike_distance keeps the last value unless it changes. I want to set the value of my sensor.lightning_strike_history to 0 every midnight regardless of what sensor.gw1100b_lightning_strike_distance has.
(btw: thanks for catching that Miles thing in my unit_of_measurement. It’s been changed… I know it’s a little thing but those are the ones that bite ya later)

If you told me what the issue was when I asked that would have saved us wasting time.

I’m really not a very good mind reader.

Here, try this:

- trigger:
  - id: reset
    trigger: time
    at: "0:00"
  - id: update
    trigger: state
    entity_id: sensor.gw1100b_lightning_strike_distance
    not_to:
    - unknown
    - unavailable
  sensor:
  - default_entity_id: sensor.lightning_strike_history
    name: Lightning strike history
    state_class: measurement
    unit_of_measurement: mi
    device_class: distance
    state: >
      {% if trigger.id == 'update' %}
        {{ states('sensor.gw1100b_lightning_strike_distance') }}
      {% else %}
        0
      {% endif %}

Thank you.