Setting sensor value in automation or script

I have sensors from mobile app that shows the last push notification from delivery service and the last removed notification on mobile.

Based on this sensor I have some automations like ‘if the last notification contains “delivery is coming” then do1 and if contains “delivered” then do2’.

But sometimes app does not send the second push. So the sensor has only the first state “delivery is coming” and I have to manually set the sensor state to “delivered”. So I’d like to make a special automation or script that assigns a certain value to the sensor.

Can anyone help with the question how I can set a sensor value via script or automation?

It’s a little unclear what you are talking about. If you are talking about the sensor from the Mobile App integration, the answer is “You don’t”. But, if you are asking about an intermediary sensor, you need to share it’s configuration with us so we assess the options.

For the automation you can just add another trigger. Again, you will need to share the configuration for us to provide options that will work with what you already have.

Ok I’ll try to include automations and so on:

I have 2 sensors from mobile app - last notification and last removed notification and the automation:

trigger:
  - type: opened
    platform: device
    device_id: 141835cbe808ccb51c3d679e1e48ffe9
    entity_id: e84e6e4a2762455de5bc38241a6ac8c7
    domain: binary_sensor
condition:
  - condition: and
    conditions:
      - condition: time
        after: "09:00:00"
        before: "21:00:00"
      - condition: template
        value_template: >-
          {{ "Delivery is coming" in
          states.sensor.ne2211_last_notification.state or  "Delivery is
          coming" in states.sensor.ne2211_last_removed_notification.state}}
action:
  - service: script.delivery_greeting
    data: {}
    enabled: true

So the “normal working” case is:

  1. When I make the order I have a push on mobile “Delivery is coming”
  2. This text is transfered to the sensors
  3. When the door opens a script.delivery_greeting is fired.
  4. Then the next push “Delivered” comes to mobile and the next door open does not fire the script.

But some time the delivery app does not send the second push “Delivered” so the sensor stores position like I’m waiting for the delivery and script.delivery_greeting fires EVERY door open.

So I have to open Developers panel → States → open both sensors and manually set them states not having ‘Delivery is coming’ words.

The needed development - I need a script that clearly sets a sensor sensor.ne2211_last_notification in HA state that I can call from UI/Telegram/other automation.

Are these sensors the ones from the mobile app integration, or user-defined sensors like a Template sensor?

The sensors are standard from mobile app.

The issue is not in the sensors - they work correctly. The issue is that delivery app does not send the push that the delivery was done and I have to MANUALLY change the sensors value. I’d like to change “manually” to “pushing a single button of script” or “having automation that clears sensor value at 00.00”.

For this I need to somehow change sensor value not via Developers panel but via script

Unless the device you are using receives notifications only from the delivery app or you have removed everything but the delivery app from the allow list, I don’t think this is a reliable methodology, there’s no guarantee that those messages will be the ones that are cached in the sensor states when the door opens.

There are no services to directly change the state of most entities. However, certain user-defined entities have mechanisms that make it possible.

I would suggest setting up a trigger-based template binary sensor. That would give you an entity whose state changes based on specific notifications with the option of time-based and/or manual resetting.

Thank you - that’s the answer