How to manually set state/value of sensor?

Hi @rodpayne
I’m trying to dynamically update thermostat (climate) away_temp value using your script with no luck so far :frowning:
Nothing is happening

How to troubleshoot this and know what is wrong in my code?

Here is my automation:

##### When changing the value of input_number.away_temp, then thermostat away_temp is changed as well
- id: updating_thermostat_salon_when_away_temp_changed
  alias: Updating Thermostat Salon away_temp value when input "Temp Absent" is changed
  trigger:
  - platform: state
    entity_id: input_number.away_temp
  action:
  - service: python_script.set_state
    data_template:
      entity_id: climate.thermostat_salon
      away_temp: "{{ states('input_number.away_temp') }}"

input_number.away_temp is a switch that I can modify in lovelace.
The goal is to be able to dynamically change a climate: away_temp value by changing the value of this input…
Right now I can only initialize it at startup but cannot change it.
Here is my Climate initialization:

##### Heating Thermostat climate.salon
- platform: dualmode_generic
  name: Thermostat Salon
  heater: switch.chauffage_salon
  cooler: switch.clim_salon
  target_sensor: sensor.temperature_moyenne_du_salon
  min_temp: 14
  max_temp: 28
  #target_temp: 23
  cold_tolerance: 0.2
  hot_tolerance: 0.2
  away_temp: 18 
  precision: 0.1
  #initial_hvac_mode: "heat"
  reverse_cycle: true
  min_cycle_duration:
    minutes: 10

Any help would be much appreciated :slight_smile:

The python script you use is for setting the state of a sensor, not the state of an attribute of a climate entity. This is not going to work.
I think even if you would be able to set this attribute,it would be overwritten by the system automatically with the value configured in the climate integration that you use.

Thank you for you for your answer,

So you think there is no way to dynamically set the away_temp value of a climate integration?

There’s no way to set this as far as I know, this would need to be implemented in the clinate integration.

1 Like

Ok thank you. Will ask support in the climate integration then ! I think I’m not the only one in need of the feature :slight_smile:

Thanks a lot, this comment solved my problem! Amazing what you can find/solve through this forum.

This was a very useful thread. I discovered how to use set_state to stuff values into a broken sensor from an independent Node Red source, and I also discovered that I could use set_state to set the entity_picture of a badge.

I’m not worried about the original component code overwriting my values because it’s broken, but I made sure to disable it in its sensor.py file anyway.

Thanks for that wonderful python script works perfectly for my project

Thank you thank you thank you for sharing this.
I am not a programmer and having a template like this came very handy.

Hi. I’m trying to get the set_state script working with the speedtest sensors, 'cause I would like to have them resetted before starting a new test, but I’m failing…
I’ve tried in the developers tool services tab but nothing happens. What could I do?

service: python_script.set_state
data:
  entity_id: sensor.speedtest_download
  state: '0'
  allow_create: true

Rodpayne, after a whole day of trying to unsuccessfully pass an argument to a python script to manage a sensor, I’m desperate so your help would be much appreciated.

My python script is

Copy to clipboard

name = data.get("nombre")
logger.warning("Hello %s", name)
hass.bus.fire(name, {"wow": "from a Python script!"})

And I’m using it as follows

Copy to clipboard

type: button
entity: sensor.irrigation_icon_1
tap_action: 
  action: call-service
  service: python_script.hello_world
  data_template:
    nombre: jaime

And I’m getting the following warning

2021-06-06 02:41:13 WARNING (SyncWorker_5) [homeassistant.components.python_script.hello_world.py] Hello None

So it is clear that data_template is not working. I’ve tried everything and can’t make it work. Is there anything else I should be doing?

Thank you again anyway if you get to look at this

Rodpayne, never mind … I was able to fix it. Thanks anyway.

If anyone is interested, the proper way to pass an argument to python script is as follows

type: button
entity: sensor.irrigation_icon_1
tap_action:
  action: call-service
  service: python_script.hello_world
  service_data:
    nombre: jaime

Notice I used service_data: in place of data_template:

Is there a way to set the icon_color different for if it is set to ON or OFF?

hi mate, I think im missing someting, can you please tell me what to add it to conf yml to enable the py script? may be an example of your one.
It does not change the state… :frowning:

i added automation:

  • id: ‘1627453170332’
    alias: set_state
    description: ‘’
    trigger:
    • platform: state
      entity_id: light.door_bell_mock_light
      from: ‘off’
      to: ‘on’
      condition: []
      action:
    • wait_template: ‘’
    • service: python_script.set_state
      data_template:
      entity_id: binary_sensor.door_bell_mock_motion_bin_sensor
      state: ON
      mode: single

my conf yml:

template:
binary_sensor:
- name: “Door_bell_Mock_Motion_bin_sensor”
state: off
device_class: motion

and also the py file into /config/python_scripts

#--------------------------------------------------------------------------------------------------

Set the state or other attributes for the entity specified in the Automation Action

#--------------------------------------------------------------------------------------------------

inputEntity = data.get(‘entity_id’)
inputStateObject = hass.states.get(inputEntity)
inputState = inputStateObject.state
inputAttributesObject = inputStateObject.attributes.copy()

newState = data.get(‘state’)
if newState is not None:
inputState = newState

newIcon = data.get(‘icon’)
if newIcon is not None:
inputAttributesObject[‘icon’] = newIcon

hass.states.set(inputEntity, inputState, inputAttributesObject)

What are you trying to achieve?
There might be new ways in the 2+ years since this thread was started…

Basically doing a door bell out of my onvif feed and ikea button
Managed to get push notification and snapshots with them when button is pressed but notification come silently if iphone is not in use :[ so i try to change state of mock motion sensor based on mock light . This in turn will trigger native ios notification from homekit…

Not sure if there is a better way to get solid notification to iphone even if is not in use … trigger when a mock light is on…. Hope it shed some light on my project

template:
  - binary_sensor:
      - name: "Door_bell_Mock_Motion_bin_sensor"
        device_class: motion
        state: "{{ states('light.door_bell_mock_light') }}"
1 Like

sorry, deleted

Try to send the message as critical one. Note it will play the sound even if night mode is enabled

1 Like

I’m a bit stukc with this. I created a folder called python_scripts
inside that I put the text file set_state.py
I installed PYScript from HACS
I restarted
Added the line to configuration.yaml:

# python
python_script: set_state.py

But when I do check configuration I get this:
Invalid config for [python_script]: expected dict for dictionary value @ data[‘python_script’]. Got ‘set_state.py’. (See ?, line ?).

I tried it in the configuration.yaml without the .py but it still is no good.
Any ideas what I’ve missed? the documentation around putting the line into configutation.yaml isn’t very helpful