How to manually set state/value of sensor?

python scripts go into their own folder in your config directory.

see here

Is this correct?


This is how it looks from my Windows PC
I had to created the python_scripts folder myself

looks right.

but in your configuration.yaml you should now have this:

python_script:

with nothing else in the same line

Thanks my mistake was that I thought you had to declare the name of the file after python_script:

1 Like

the line in your config.yaml is just a placeholder to tell HA to look there for a list of python scripts.

all of the python scripts go into that same folder.

1 Like

Ok So I have it working now with the script :slight_smile:
However I am trying to fix the issue affecting RFLink devices I have since HASS updated and now allows Unknown Binary Sensor states when HASS is rebooted

So with that in mind I need to turn this into a list of devices.
I have tried but every time I add a device the script editor removes it.
Any idea how to make this a multi line multi entity script so I can set the state of 12 devices.

service: python_script.set_state
data_template:
  entity_id: binary_sensor.kitchen_pir
  state: 'off'
  allow_create: true

This might work:

service: python_script.set_state
data:
  entity_id: 
    - binary_sensor.kitchen_pir
    - binary_sensor.some_other_one
    - binary_sensor.etc
  state: 'off'
  allow_create: true

But I haven’t tested it so I don’t know if the python script accepts lists of entities like that as other service calls do.

If that doesn’t work then I think you might have to do it as individual service calls for each entity.

- service: python_script.set_state
  data:
    entity_id: binary_sensor.kitchen_pir
    state: 'off'
    allow_create: true
- service: python_script.set_state
  data:
    entity_id: binary_sensor.some_other_one
    state: 'off'
    allow_create: true
- service: python_script.set_state
  data:
    entity_id: binary_sensor.etc
    state: 'off'
    allow_create: true

I did individual service calls but now I have the following problem,
When state is changed by the python script a sensor detecting motion does not change the state to detected.

I used the python script to set the state from “unknown” to “off” it just gets stuck in off.
If I change it to on it gets stuck in on.

Not sure if its related to the RFlink protocol I’m using as I’m thinking that the RFLink is sending the state out to the sensor potentially

it’s going to stay in the same state that you set it to until the integration sends out another update to set the state to the true state of the entity.

Maybe I’m not understanding the ultimate issue you are trying go solve with the python script.

Ultimate issue is since 2 x versions ago binary_sensor got a new state unknown so my Yale alarm sensors which are linked to HA using a RFLink come up in HA like this after every reboot:

The ones showing clear have had a single motion event which has now cleared which is why they now show a state of clear

The sensors which are door \ panic will never change until a detection takes place. I don’t like them showing as unknown as it worries me that they are “not connected”

Running the python script with multiple service calls I’m able to get my sensors into this state … But then they never change even though I’m sure events are being sent to HA

Unavailable means they are not connected.

Yeah, that’s too many moving parts that I’m not familiar with.

But I don’t see why manually setting the state outside of the integration would somehow block the integration from ever updating the entity again in the future.

you can also test it by setting the state manually in the dev tools → states page by selecting the entity and then changing it’s state there.

It should update on the next event after that change as well.

And TBH, I don’t know that changing the state manually should alleviate your concerns that the sensors aren’t connected either. Since you are changing them they will then stay as that state until the integration updates them. If the sensor isn’t connected it will stay in that “clear” state forever and you won’t know it.

at least if it says “unknown” you can tell if it’s ever triggered since the last restart.

I have a feeling that doing the state change to “OFF” actually transmits codes to my yale sensors which may “block” them detecting motion as I know when you change the keyfob status on HA it acually transmits the arm \ disarm state on 433.92 band which was controlling my old control panel before i dumped it in a skip.

In any case @petro I totally get that unavailable is a state where the sensors are not connected I might just have to ignore the new state :open_mouth:

Strangely if I restart the RFLink module it reverts the sensors back to Unknown.

The python script does not transmit anything back to yale. It just sets the state in the current state machine.

Yes @petro but changing the state of the sensor will probably do a transmit action to the sensors orr could even block the RFLink box which is connected via USB serial port

One of the things I learnt about RFLink is that once it learns a code (particulary from a fob) it re-transmits that code on a status change. This is how I can use the yale sirens with HA and trust me that was hard to get my head round

No, that’s not how the code works. The state machine reacts to the hardware. If you overwrite the state in the state machine, nothing happens other than writing the state in the state machine.

1 Like

For Node-Red use a call service node with domain = python_script, service is the name of script (which is set_state for above script), and for data use JSON similar to below:

{
    "entity_id": "sensor.washing_mach_flood_water_alarm_sensor_status",
    "state": "idle",
    "value": 0,
    "icon": "mdi:water-outline"
}

For updating any entity attributes, make sure you grab the updated script. It was a little further down in the comments.

Thanks Rod, solved a problem!

Hello All,

I’m trying to set my Kitchen shades state to OFF using this Python script and getting the below error. Can someone please help me, if I’m doing something wrong?

it should be data, not data_template if I’m not mistaken