How to reset the "current" value of a sensor when HA restart

i have a sensor for the battery %, which is setted via an automat with a Webhook from another system.

my sensor :

  - sensor:
      - name: "pourcent_portail_batterie_aeotec"
        unique_id: "pourcent_portail_batterie_aeotec01"
        unit_of_measurement: "%"
        device_class: battery
        state_class: measurement
        state: >
          {{ "0"}}

and the automat

- id: '1705240855162'
  alias: reçois % batterie aeotec portail
  description: ''
  trigger:
  - platform: webhook
    allowed_methods:
    - POST
    - PUT
    - GET
    local_only: true
    webhook_id: pourcent_batterie_aeotec_portail
  condition: []
  action:
  - service: python_script.set_state
    data_template:
      entity_id: sensor.pourcent_portail_batterie_aeotec
      state: '{{ (trigger.query.pourcent_portail_batterie_aeotec) | float }}'
  mode: single

each time i restart HA the sensor is reset to “0”
i havn’t found how to reset the sensor at the last value before restart ?

Why are you using such a convoluted method instead a just using a trigger-based template sensor… which would have the added benefit of surviving restart…

1 Like

hi @Didgeridrew , of course, could you give me the exemple on how to simplify this

nota: i just tried to find a way to make the job, but obviously it’s not the best one, and i didn’t find a service that could update basically the content of a sensor.

i have found now i remove all that was setted before (automation and sensor) and setted under template.yaml

  - trigger:
    - platform: webhook
      allowed_methods:
      - POST
      - PUT
      - GET
      local_only: true
      webhook_id: pourcent_batterie_aeotec_portail
    sensor:
    - name: "Pourcent_portail_batterie"
      unique_id: Pourcent_portail_batterie_test_02
      unit_of_measurement: "%"
      state: >
          {{ (trigger.query.pourcent_portail_batterie_aeotec) | float | round(0) }}

1rst test is OK, the % is setted when the trigger is armed and after a restart of HA the value still stay.