Input slider values to a python script for RGB LED

Hello,

I’m still pretty new to HA but I’m trying to send the input_slider values to a python script. With this python script I’m able to change the RGB values of a LED. Currently it’s set to a fixed value so you can it a scene.

This is my configuration:

#slider
input_slider:
slider1:
name: Rood_living
initial: 2048
min: 0
max: 4095
step: 5
slider2:
name: Groen_living
initial: 2048
min: 0
max: 4095
step: 5
slider3:
name: Blauw_living
initial: 2048
min: 0
max: 4095
step: 5

switch configuration

  • platform: command_line
    switches:
    ledstrip:
    command_on: python /var/opt/homeassistant/test.py 0 2000 4095 --> thes values should come eventually from a slider change.
    command_off: python /var/opt/homeassistant/uit.py

automation config.

  • alias: dim licht
    trigger:
    • platform: state
      entity_id: input_slider.slider2
      action:
      service: notify.Glenn
      data:
      message: {states.input_slider.slider2} --> was trying to see if I can send the state value with pushbullet
      title: “test2”

So I want smooth change of the RGB LED with a slider value change and this togehter with a pyton script;

test.py states.input_slider.slider1 states.input_slider.slider2 states.input_slider.slider3

Hi,
i publish the values from the sliders to mqtt and let the python script read them from it.
Automation:

- alias: Milight3 color slider
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_slider.milight3_color
  action:
    - service: mqtt.publish
      data_template:
        topic: 'home/milights/milight3/color'
        payload: '{{ trigger.to_state.state | int }}'
        retain: 'true'

a simple python example for getting the values:

#!/usr/bin/python

import paho.mqtt.client as mqtt
import sys

topic = "home/milights/milight3"

def on_connect(client, userdata, flags, rc):
    #print("Connected with result code " + str(rc))
    client.subscribe(topic + "/#")

def on_message(client, userdata, msg):
    #print(msg.topic + " " + str(msg.payload))
    dict[msg.topic.split('/')[-1:][0]] = str(msg.payload)
    if len(dict) == 6:
        client.disconnect()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

dict = {}
client.connect("localhost", 1883, 60)
#client.publish(topic + "/state", state, 0, True)
client.loop_forever()

#print(dict)
print('  State: ' + dict['state'])
print('CSwitch: ' + dict['cswitch'])
print('  Color: ' + dict['color'])
print('  Satur: ' + dict['satur'])
print(' Bright: ' + dict['bright'])
print('   Temp: ' + dict['temp'])

The script reads 6 values from topic home/milights/milight3 and prints them.
I made this with snippets from googling and try and error.
Maybe anyone knows a better way.

Greetings

I think you just need to add .state to your sliders …
so …
message: {states.input_slider.slider2.state}

same thing for your python commands…
command_on: ‘python /var/opt/homeassistant/test.py 0 {{states.input_slider.slider1.state}} {{states.input_slider.slider2.state}}’

You may not need two {{}} round each state and you may not need the single quote round the whole line, but it works for me :slight_smile:

Hi,

Thanks for the fast replies, I will try to implement the solutions and keep it updated for somebody else :slight_smile:

regards,

G

Hello first of all thank you,but … :slight_smile:

I still have some issues especially with the pyton script :confused:


#!/usr/bin/python

import paho.mqtt.client as mqtt
import sys

topic = “home/milights/milight3”

def on_connect(client, userdata, flags, rc):
#print("Connected with result code " + str(rc))
client.subscribe(topic + “/#”)

def on_message(client, userdata, msg):
#print(msg.topic + " " + str(msg.payload))
dict[msg.topic.split(’/’)[-1:][0]] = str(msg.payload)
if len(dict) == 6:
client.disconnect()

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

dict = {}
client.connect(“localhost”, 1883, 60)
#client.publish(topic + “/state”, state, 0, True)
client.loop_forever()

I think I modified it but I’m not sure when the script will be called and if need to change the topic as well. Is there also a change in the configuration.yaml?

once again I’m new to this so sorry if I’m asking strange questions.

regards G

Hi,

the script is just an example of getting values from mqtt.

I think the solution from keithh666 would be the better way.

In your first post you have a command_line switch.
You can turn it on when slider changes with:

- alias: Milight3 color slider
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_slider.milight3_color
  action:
    - service: mqtt.publish
      data_template:
        topic: 'home/milights/milight3/color'
        payload: '{{ trigger.to_state.state | int }}'
        retain: 'true'
    - service: switch.turn_on
      data:
        entity_id: switch.milight3

So the command_on: from your switch will be executed.
Greetings
Rainer

Hi Guys,

I’m one step further thanks btw but I think you misunderstood my question. I do not want to run the script on a slider change but I want to send the state value of the slider to my python script. I have 3 sliders that stands for the 3 colors in my strip.

I’m able to send the input_Slider value to my phone with pushbullet, I already think this a pretty big achievement :slight_smile:.

Code:

- alias: dim licht Groen message
  trigger:
  - platform: state
    entity_id: input_slider.slider2
  action:
    service: notify.Glenn
    data:
      message: '{{states.input_slider.slider2.state}}'
      title: "slider" 

Cool, now you only have to change your python script to recieve the push messages, and you can control the LED around the world.:smile: (joke)

Do you mean your script runs in a loop and should recieve the values?
So, then i think the mqtt solution could work.

You need a mqtt broker -> https://home-assistant.io/components/mqtt/

Setup the automation like in my first post

- alias: dim licht Groen message
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_slider.slider2
  action:
    - service: mqtt.publish
      data_template:
        topic: 'your/topic/here/slider2'
        payload: '{{ trigger.to_state.state | int }}'
        retain: 'true'

If you start the following python script in a console, you can see the values from the slider live.

#!/usr/bin/python

import paho.mqtt.client as mqtt

topic = "your/topic/here"

def on_connect(client, userdata, flags, rc):
    client.subscribe(topic + "/#")

def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))
    #with the msg.payload you can control the LED

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 60)
client.loop_forever()

Hope you have success with this!

Rainer

Hi Rainer,

Thanks again, I do understand the script and also the config in the automation :slight_smile: but I hope you can guide me once more (sorry for the stupid questions)

I modified the script a little to get it work:

automation.yaml

- alias: dim licht Groen message
  trigger:
  - platform: state
    entity_id: input_slider.slider2
  action:
    service: mqtt.publish
    data_template:
        topic: dev/test/slider2
        payload_template: '{{states.input_slider.slider2.state | int }}'
        retain: true

script:

#!/usr/bin/python

import paho.mqtt.client as paho
import paho.mqtt.publish as publish

topic = "dev/test/slider2"

def on_connect(client, userdata, flags, rc):
    client.subscribe(topic + "/#")

def on_message(client, userdata, msg):
    print(msg.topic + " " + str(msg.payload))
    #with the msg.payload you can control the LED

client = paho.Client()
client.on_connect = on_connect
client.on_message = on_message

client.connect("localhost", 1883, 60)
client.loop_forever()

But if I run it in the terminal of the PI it stays empty even when I move the slider. The script is saved in the same folder as where the configuration.yaml is saved (if it matters)

one small note:

This is my setup of the mqqt broker in configuration.yaml:

mqtt:
  broker: 10.10.1.117
  port: 1883
  client_id: home-assistant-1
  username: username
  password: password

also I have sensors working on MQTT.

Sorry, why not just have an automation that calls the python script anytime the slider value changes? And pass the slider value to the script. If you want more info let me know…

Change topic = "dev/test/slider2" to topic = "dev/test"

The line client.subscribe(topic + "/#") adds the "/#" to it. This is a wildcard in mqtt, so every topic under test will be shown.

dev/test/slider1
dev/test/slider2
dev/test/slider3
and so on ...

If your payload template works, the script should show the values.

Good luck!

Edit:
if your mqtt broker is not on the same host, you have to change the connect statement.
client.connect("10.10.1.117", 1883, 60)

Hey stunts1337,

I would like to know how to do it, for now I’m going to apply the MQTT method.

regards

action:
  - service: shell_command.call_script
    data_template:
      colorvalue: '{{states.input_slider.slider2.state | int}}'

shell_command:
  call_script: python path/to/script.py {{colorvalue}}

And in the python script retrieve it…

import sys
value = sys.argv[1]
1 Like

Hi Stunts1337,

Thanks it works but actually I want to send 3 values for 1 it works or for 3 also but then the value is coming from the same slide. Do I need to make 3 shell commands for it ?

automation.yaml

- alias: dim licht Rood message
  trigger:
  - platform: state
    entity_id: input_slider.slider1
  action:
    service: shell_command.call_script
    data_template:
         rood: '{{states.input_slider.slider1.state | int}}'
- alias: dim licht Groen message
  trigger:
  - platform: state
    entity_id: input_slider.slider2
  action:
    service: shell_command.call_script
    data_template:
         groen: '{{states.input_slider.slider2.state | int}}'
- alias: dim licht Blauw message
  trigger:
  - platform: state
    entity_id: input_slider.slider3
  action:
    service: shell_command.call_script
    data_template:
         groen: '{{states.input_slider.slider3.state | int}}'

configuration.yaml

shell_command:
  call_script: python /var/opt/homeassistant/test.py {{rood}} {{groen}} {{blauw}} 

This works but as you can see only 1 value is changeable instead of 3. (4095 is off, 12 bit PWM board )

shell_command:
  call_script: python /var/opt/homeassistant/test.py {{rood}} 4095 4095

Try just adding more data lines in one automation:

data_template:
  groen: '{{states.input_slider.slider1.state | int}}'
  rood: '{{states.input_slider.slider2.state | int}}'
  blauw: '{{states.input_slider.slider3.state | int}}'

Then you can have multiple triggers that trigger the script

trigger:
  - platform: state
    entity_id: input_slider.slider1
  - platform: state
    entity_id: input_slider.slider2
  - platform: state
    entity_id: input_slider.slider3

hi,

If I do that my home assistant is not reachable anymore. --> sorry made a typo, it works :slight_smile:

- alias: dim licht
  trigger:
  - platform: state
    entity_id: input_slider.slider1
  - platform: state
    entity_id: input_slider.slider2
  - platform: state
    entity_id: input_slider.slider3
  action:
    service: shell_command.call_script
    data_template:
         rood: '{{states.input_slider.slider1.state | int}}'
		 groen: '{{states.input_slider.slider2.state | int}}'
		 blauw: '{{states.input_slider.slider3.state | int}}'

Thank you all, I’ll post later my configuration to maybe help others and maybe some people have better ideas how to this nevertheless I still want to use MQTT. So Rainer thnks for the help I do understand more how it works and will use this for my sensor setup.

Kind regards,

Glenn

1 Like