How to manually set state/value of sensor?

make a binary_sensor instead w/ on/off, and set the auto_off to a short value.

  - trigger:
      - platform: event
        event_type: "imap_content"
        id: "custom_event"
          sender: "[email protected]"
      - name: Sensor_name
        state: "{{ 'specific phrase' in }}"
          milliseconds: 50
1 Like

Thanks for that but I have a bit of logic that goes on once the email has been received and I want to reset the sensor only if and after that logic has been processed.

For now, I have installed the setter integration (note you have to replace the file to get it to load successfully). This has the advantage of creating a service I can call from NodeRed as I want.

That’s still a template sensor of some sort, with an additional trigger. You want to avoid this whole entire thread. It’s just filled w/ people using a work-around because they are unwilling to learn the way HA updates entities.

1 Like

Must admit I suspect there’s an approach using templates that might work, that’s what I’ll be investigating next as setter has already needed a fix to keep it working with recent versions of HA. I prefer to do things within main HA if possible. If you could point me to a good starting point for templates I’d appreciate it.

Well, just explain what you want “this advanced” logic to do. I’ll share how I would approach the problem.

That all happens in NodeRed and I’m happy with how that works. The trigger is the receipt of an email the day before (if you’re really interested, you can read about it here). I can’t be sure when the email arrives so on its receipt, I set a helper which I use to trigger my NodeRED flows the next day. When the email has been processed, and in my current logic at 7pm, I want to reset the email received sensor so, if another one arrives the next day, the sensor will be set. Hope this makes sense.

So all I want is a way of resetting the email sensor at a time of my choosing, which might change, and idealy through NodeRED.

I use this script, but I did the installation manually, I didn’t use hacs.

[{"id":"c76fa704308a55f5","type":"api-call-service","z":"aa7ff1ce198b9fc9","name":"Indisponível","server":"33803899.487d48","version":5,"debugenabled":false,"domain":"python_script","service":"set_state","areaId":[],"deviceId":[],"entityId":[],"data":"{\"entity_id\":\"{{payload}}\",\"state\":\"{{state}}\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":430,"y":340,"wires":[[]]},{"id":"7d330ecb0be846ee","type":"inject","z":"aa7ff1ce198b9fc9","name":"","props":[{"p":"payload"},{"p":"state","v":"your_value","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"binary_sensor.pir_corredor","payloadType":"str","x":250,"y":340,"wires":[["c76fa704308a55f5"]]},{"id":"33803899.487d48","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
1 Like

There is nothing to adjust in the script. The actions are all specified in the automation that calls the script. For example:

    service: python_script.set_state
      entity_id: binary_sensor.sensor1
      state: off

(Sorry for the slow response, I have been away from HA for quite a while.)

1 Like

hello how do i add this script into homeassistant ? i cant find the folder python

What is the problem you’re trying to solve? Manually forcing states into sensors is unlikely to be the right way to do it.

i have a sensor [

SONOFF SNZB-03 ZigBee Motion Sensor

](SNZB-03 - SONOFF Official)

the cool down of the sensor is to long so i want to use this code, for example the sensor detect motion and after 5 seconds it will reset to no motion.

Create a trigger-based template binary sensor with a state trigger looking for your motion sensor going on and an auto_off of 5 seconds.

Changing that doesn’t mean the sensor will produce more motion events. In all likeliness, you’ll have the same number of motion events but your sensor now turns off after 5 seconds. You should adjust the settings in the hardware, not use a ill-though-out band aide.


I have red a lot of posts, but probably not the right one…

I want to use a value from a sensor to be written via to the other entity, like:
in the automation i wrote:
state: {{ states(‘sensor.power_pv_total’)}}
entity_id: sensor.ccu3_webui_sv_current_plant_power

But I am struggeling with the syntax to do it right.
Can someone help out here.



Why do you want to go this (wrong) way, rather than just setting up a template sensor?

  - sensor:
      - name: "Copy of other sensor"
        state: "{{ states('sensor.power_pv_total') }}"

What are you actually really trying to do? See sections 8 and 11 of this guide:

Got it, thanks.

Thanks, works like a charm!

service: python_script.set_state
  entity_id: sensor.ccu3_webui_sv_current_plant_power
  state: "{{ states('sensor.power_pv_total') }}"

and/or to change attribute(s):

  entity_id: sensor.ccu3_webui_sv_current_plant_power
  state: "{{ states('sensor.power_pv_total') }}"  # optional if attribute(s) are listed
  name_of_attribute: "some_value"
  brightness: "{{ state_attr('switch.adaptive_lighting_bathroom_light','brightness_pct') }}"
  current_tilt_position: "{{ state_attr(trigger.entity_id, 'current_position') }}"
  nighmode: "on"
  message: "Some string message"
  image: "/path/image_name"
  timer: etc...

Note though that the state|attributes written by the script does not survive a system restart nor does it survive any state updates which occur through the integration which created sensor.ccu3_webui_sv_current_plant_power.

1 Like

This works perfectly thank you! My android TV only has a state of off or on. This script allows me to set a status of playing or paused, to better control the TV!


Thanks so much for this, It took me a ages to finally work out my problem.

Basically I have a Switchbot Bot in toggle mode. So when I turn it on the bot then presses the button and then turns back, and this is not reflected in Home Assistant.

So with this I was able to create an automation that would reset the bot switch back to “off” so when the user looks at it the status in Home assistant or as I have it exported to homekit, HomeKit.

alias: Office - Revert bot state to OFF
description: ""
  - platform: state
      - switch.switchbot_bot
    to: "on"
condition: []
  - service: python_script.set_state
      entity_id: switch.switchbot_bot
      state: "off"
mode: single

When I initially tried this, I just called the swithc service and that would cause the bot to press twice when it reset the switch to the proper mod, with this I just get a single press.

Thanks again.