How to manually set state/value of sensor?

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.

Hi,

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 set_state.py 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.

Regards

Gerhard

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

template:
  - 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
data:
  entity_id: sensor.ccu3_webui_sv_current_plant_power
  state: "{{ states('sensor.power_pv_total') }}"

and/or to change attribute(s):

data:
  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!

Hi,

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: ""
trigger:
  - platform: state
    entity_id:
      - switch.switchbot_bot
    to: "on"
condition: []
action:
  - service: python_script.set_state
    data:
      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.

Once the original issue came back (intensively the last days) I tried that, unfortunately after adjusting the sensor definition and reloading template entities, the template sensor rendered unknown. Any idea why?

Here’s my full definition:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.vibration_xyz
        id: "sensor"
      - platform: state
        entity_id: binary_sensor.cover_xyz
        to: "on"
        for: "00:30:00"
        id: "stuck"
    binary_sensor:
      - name: "Cover XYZ"
        # entity_id: binary_sensor.cover_xyz
        unique_id: bliblablub
        device_class: opening
        delay_off:
          seconds: 1
        state: >
          {% if trigger.id == "sensor" %}
            {% if (state_attr('binary_sensor.vibration_xyz', 'orientation') [2]) | int(0) > -70 %}
              {{ 'on' }}
            {% else %}
              {{ 'off' }}
            {% endif %}
          {% else %}
            {{ 'off' }}
          {% endif %}
        availability: "{{ is_state('binary_sensor.vibration_xyz', 'on') or is_state('binary_sensor.vibration_xyz', 'off') }}"
        auto_off:
          minutes: 30

Update: once a state change happened, the sensor switches state (e. g. to on). So I need some kind of proper default initialization as after every template sensor reload/HA restart the state is unknown until the first input sensor state change happens (which could take hours). How to achieve that?

If that could be solved and the “stuck” trigger (currently monitoring it) is working too, I’d consider this a proper/smart solution. Thanks @Troon and @petro.

template entities restore state, so it will only be unknown when you build it the first time. Restarting will cause it to restore a state. Reloading it will flop back to unknown.

FYI, simplified.

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.vibration_xyz
        id: "sensor"
      - platform: state
        entity_id: binary_sensor.cover_xyz
        to: "on"
        for: "00:30:00"
        id: "stuck"
    binary_sensor:
      - name: "Cover XYZ"
        # entity_id: binary_sensor.cover_xyz
        unique_id: bliblablub
        device_class: opening
        delay_off:
          seconds: 1
        state: >
          {{ trigger.id == "sensor" and state_attr('binary_sensor.vibration_xyz', 'orientation')[2] > -70 }}
        availability: "{{ 'binary_sensor.vibration_xyz' | has_value }}"
        auto_off:
          minutes: 30

First: thanks for optimizing things, much easier to read now :slight_smile:

Ahm that’s definitely a new behaviour.

  • I use several hundreds of template sensors and I did not see that with the old template sensor definition (without the newly added trigger part), I could reload template sensors as often I want, it always had it’s correct value directly and never was unknown.
  • And I also did not build it for the first time, keeping the UID the sensor is well known to the state machine already, basically only the trigger part was added.

Old definition (only state part):

#        state: >
#          {% if (state_attr('binary_sensor.vibration_xyz', 'orientation') [2]) | int(0) > -70 %}
#            {{ 'on' }}
#          {% else %}
#            {{ 'off' }}
#          {% endif %}

Any recommendations?

Either that’s a bug with trigger based template entities or I need to somehow work around that (which leads back to the original question: “how to manually set state/value of sensor?” :slight_smile: as I have a monitoring system reporting unknown and unavailable entities. Simply suppressing the warning for this template sensor is just the last resort. I think I really want to set the state correctly when reloading/initializing the template sensor. Like it always did.

No it’s not. This behavior has always existed for trigger based template entities.

That’s what I’m talking about. In plain text: this is the first time I use trigger based template entities.

…and I really don’t like that unknown state behaviour. Why? It has all information to render the actual state. I guess it simply waits for one of the triggers to actually…well, trigger - in this two trigger case for a state change of the input sensor. Somehow logical.

So how can I motivate the sensor to leave its unknown state? Thinking bout a fix-automation like “when sensor switches to unknown, do ”.
If I could set an entities state, I’d read the input sensor’s state and simply set the same once again, hoping this will trigger the trigger based template sensor to render fine. Aaaaaaah well everything get’s so complicated under the hood, doesn’t it.

Well, at least the “stuck” trigger is working, tested after 30 minutes now. So I swapped one deficiency (stuck sensor) with a new issue (unknown sensor).


Update:

Forget it. After reloading the sensor two more times (after applying the state and availability template optimizations of petro), there’s now unknown state anymore. Seems to work, hopefully :slight_smile:

It does not, because you aren’t providing triggers for it.

Trigger based template entities only update when a trigger occurs. You don’t have a trigger for when it reloads, you don’t have a trigger for when you restart. So it gets assigned an unknown state when either of those things occur.

(FYI: I updated my previous post)
Just to learn another thing: what does a trigger for

  • reload
  • restart

look like?

  - trigger:
      - platform: homeassistant
        event: start
      - ...

I’m only aware of automation reload event. Template entities reload event?

1 Like

Hello,
i added the Attribute “last_room” to my person.ben
how can i set the room/area with states('sensor.ben_iphone_ble_area') to my person.ben ?

configuration.yaml:

homeassistant:
  customize_domain:
    person:
      last_room: ""

I Hope someone can help me… thx