• 10 dec 2017: forum version update. In case of issues use this topic.
  • 30 nov 2017: pilight moved servers. In case of issues use this topic.
Hello There, Guest! Login Register


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Always trigger event on any device state
#1
I like to use the on/off state of a simple wall switch (kaku) to increase or decrease a value.

So by pressing on 3 times the value becomes 30% and tapping off 1 time it's 20%.

The problem is that when a device state is switched to on pilight won't trigger a event again when pressing on again.

Is there some solution for this possible?
 
Reply
#2
(03-21-2017, 05:24 PM)Phil Wrote: The problem is that when a device state is switched to on pilight won't trigger a event again when pressing on again.
This is correct, but if you look closer, you will see that pilight-receive still picks up the message. Which means that...

(03-21-2017, 05:24 PM)Phil Wrote: Is there some solution for this possible?
... this is certainly possible. You'll need to use some external script to interact with the pilight socket API and react to repeated button presses, and then change the value of the dimmer device accordingly. Shouldn't be too difficult Smile
 
Reply
#3
(03-21-2017, 06:59 PM)pilino1234 Wrote:
(03-21-2017, 05:24 PM)Phil Wrote: The problem is that when a device state is switched to on pilight won't trigger a event again when pressing on again.
This is correct, but if you look closer, you will see that pilight-receive still picks up the message. Which means that...

(03-21-2017, 05:24 PM)Phil Wrote: Is there some solution for this possible?
... this is certainly possible. You'll need to use some external script to interact with the pilight socket API and react to repeated button presses, and then change the value of the dimmer device accordingly. Shouldn't be too difficult Smile

I was hoping for some simple option to add to the the device in the config..

pilight socket API, seems like a lot of hussle to start with, is there any example python script or such?

My alternative is, to read the stdout of pilight-receive to my python script and program a shit load of crap to intercept the messages.
 
Reply
#4
(03-21-2017, 07:21 PM)Phil Wrote:
(03-21-2017, 06:59 PM)pilino1234 Wrote:
(03-21-2017, 05:24 PM)Phil Wrote: The problem is that when a device state is switched to on pilight won't trigger a event again when pressing on again.
This is correct, but if you look closer, you will see that pilight-receive still picks up the message. Which means that...

(03-21-2017, 05:24 PM)Phil Wrote: Is there some solution for this possible?
... this is certainly possible. You'll need to use some external script to interact with the pilight socket API and react to repeated button presses, and then change the value of the dimmer device accordingly. Shouldn't be too difficult Smile

I was hoping for some simple option to add to the the device in the config..

pilight socket API, seems like a lot of hussle to start with, is there any example python script or such?

My alternative is, to read the stdout of pilight-receive to my python script and program a shit load of crap to intercept the messages.
The socket API is in fact very easy to use, programmatically you would set it up like any other socket communication, but the logic to handle the communication is so much easier. See the pilight manual page about the socket api for the documentation.

In python, you only need to import the socket module to get started with this. There are example clients in the pilight repo already, although the python version is admittedly kinda bad. I've reworked it a bit, as well as ported it to python 3, here: https://github.com/pilino1234/pilight/bl...rocess.py3. I'll make a PR of this at some point in the future.
This example shows how to get the type of data pilight-receive shows, and prints it in an infinite loop. The first half deals with the SSDP discovery of pilight daemons on the local network, you can probably skip that and start from line#57 for logic.
 
Reply
#5
(03-21-2017, 07:27 PM)pilino1234 Wrote:
(03-21-2017, 07:21 PM)Phil Wrote:
(03-21-2017, 06:59 PM)pilino1234 Wrote:
(03-21-2017, 05:24 PM)Phil Wrote: The problem is that when a device state is switched to on pilight won't trigger a event again when pressing on again.
This is correct, but if you look closer, you will see that pilight-receive still picks up the message. Which means that...

(03-21-2017, 05:24 PM)Phil Wrote: Is there some solution for this possible?
... this is certainly possible. You'll need to use some external script to interact with the pilight socket API and react to repeated button presses, and then change the value of the dimmer device accordingly. Shouldn't be too difficult Smile

I was hoping for some simple option to add to the the device in the config..

pilight socket API, seems like a lot of hussle to start with, is there any example python script or such?

My alternative is, to read the stdout of pilight-receive to my python script and program a shit load of crap to intercept the messages.
The socket API is in fact very easy to use, programmatically you would set it up like any other socket communication, but the logic to handle the communication is so much easier. See the pilight manual page about the socket api for the documentation.

In python, you only need to import the socket module to get started with this. There are example clients in the pilight repo already, although the python version is admittedly kinda bad. I've reworked it a bit, as well as ported it to python 3, here: https://github.com/pilino1234/pilight/bl...rocess.py3. I'll make a PR of this at some point in the future.
This example shows how to get the type of data pilight-receive shows, and prints it in an infinite loop. The first half deals with the SSDP discovery of pilight daemons on the local network, you can probably skip that and start from line#57 for logic.

Tnx, i use python2 only changing the arguments of bytes made it work, this saves me a lot of time!
 
Reply
#6
Well, the python 2 example is there too, just head to the parent dir and look at process.py instead of process.py3 Wink

Nice to hear that you got it to work!
 
Reply
#7
(03-21-2017, 08:24 PM)pilino1234 Wrote: Well, the python 2 example is there too, just head to the parent dir and look at process.py instead of process.py3 Wink

Nice to hear that you got it to work!

Works great, using this switch:
KAKU Switch

I can now turn the radio (left button) on/off and turn the volume (right button) up/down.

Need another one now to switch between internet radio channels ;P
 
Reply
#8
If you want to, you can post the code (or the relevant section) that you've written to do this here, that would be very helpful for people who are looking for this in the future Smile
 
Reply
#9
(03-22-2017, 06:24 AM)pilino1234 Wrote: If you want to, you can post the code (or the relevant section) that you've written to do this here, that would be very helpful for people who are looking for this in the future Smile

Sure, keep in mind my switch is this one:

KAKU Switch

And the ID is 23223586 and the units are 10 and 11, you need to adjust this.

This python program starts at boot, after pilight is started.

First thing it does is start mplayer in slave mode then it endless loops searching for the signals of the switches.

here is my code:

Code:
#!/usr/bin/env python
#
# Copyright (C) 2016 pilino1234
#
# This file is part of pilight.
#
# pilight is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# pilight is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pilight. If not, see    <http://www.gnu.org/licenses/>
#

import socket
import struct
import re
import subprocess
import os, stat

def discover(service, retries=1):
    group = ("239.255.255.250", 1900)
    message = "\r\n".join([
        'M-SEARCH * HTTP/1.1',
        'HOST: {0}:{1}'.format(*group),
        'MAN: "ssdp:discover"',
        'ST: {st}', 'MX: 3', '', '']).format(st=service)
    ssdp_responses = {}
    i = 0
    for _ in range(retries):
        i += 1
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, struct.pack('LL', 0, 10000))
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
    sock.sendto(bytes(message), group)
        while True:
            try:
                ssdp_responses[i] = sock.recv(1024).decode("utf-8")
                break
            except socket.timeout:
                break
            except IOError:
                print("no pilight ssdp connections found")
                break
    return ssdp_responses.values()

def krijgvolume():
    proc = subprocess.Popen(["amixer get Master|grep -o [0-9]*%", ""], stdout=subprocess.PIPE, shell=True)
    (out, err) = proc.communicate()
    return int(out[ 0 : 0 + len(out) - 2])

#start mplayer
if os.path.isfile("/home/pi/.mplayer/fifo")==False:
    os.system("mkfifo /home/pi/.mplayer/fifo")

if stat.S_ISFIFO(os.stat("/home/pi/.mplayer/fifo").st_mode)<1:
    os.system("mkfifo /home/pi/.mplayer/fifo")

check = os.system("pidof mplayer")

if check==256:
    check = subprocess.Popen(['mplayer -slave -idle -input file=/home/pi/.mplayer/fifo -softvol-max 100 > /dev/null 2>&1'], stdout=subprocess.PIPE, shell=True)

responses = discover("urn:schemas-upnp-org:service:pilight:1")

if len(responses) > 0:
    locationsrc = re.search("Location:([0-9.]+):(.*)", str(responses[0]),
                            re.IGNORECASE)
    if locationsrc:
        location = locationsrc.group(1)
        port = locationsrc.group(2)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    socket.setdefaulttimeout(0)
    s.connect((location, int(port)))
    s.send('{"action": "identify", "options": {"receiver": 1}}\n')
    text = ""
    findstr1 = '"id":23223586,"unit":11,"state":"on"'
    findstr2 = '"id":23223586,"unit":11,"state":"off"'
    findstr3 = '"id":23223586,"unit":10,"state":"on"'
    findstr4 = '"id":23223586,"unit":10,"state":"off"'
    while True:
        line = s.recv(1024)
    #print("line: " + line)
    if line.find(findstr1)>0:
        value = krijgvolume()
        value = value + 1
        if value>100:
            value = 100
        os.system("amixer sset Master,0 " + str(value) + "% > /dev/null 2>&1")
    if line.find(findstr2)>0:
        value = krijgvolume()
        value = value - 1
        if value<0:
            value = 1
        os.system("amixer sset Master,0 " + str(value) + "% > /dev/null 2>&1")
    if line.find(findstr3)>0:
        subprocess.Popen(['echo \"loadfile http://live.icecast.kpnstreaming.nl/skyradiolive-SRGSTR09.mp3\" > /home/pi/.mplayer/fifo'], stdout=subprocess.PIPE, shell=True)
    if line.find(findstr4)>0:
        subprocess.Popen(['echo \"stop\" > /home/pi/.mplayer/fifo'], stdout=subprocess.PIPE, shell=True)
        proc = subprocess.Popen(["pidof mplayer"], stdout=subprocess.PIPE, shell=True)
        (out, err) = proc.communicate()
    s.close()
 
Reply
  


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Solved] On and Off for one device in one rule Oliver 3 449 10-28-2019, 06:55 PM
Last Post: Oliver
  Sunset state check parse error dennisvo 6 480 09-27-2019, 08:34 PM
Last Post: curlymo
  Can't switch Relay device by rules: Error switch.lua:77 wobbi 6 1,103 07-31-2018, 06:25 AM
Last Post: curlymo
  Rule with&without device. One working not the other... Tamadite 9 1,398 07-24-2018, 08:55 AM
Last Post: curlymo
  Program can't be set to state running Gisto 7 1,298 07-17-2018, 12:51 PM
Last Post: Gisto
  datetime event again wupperpi 5 2,528 02-26-2017, 10:11 PM
Last Post: wo_rasp
  [Solved ]Turn off device at random time manmetdehamer 4 2,212 07-27-2016, 03:35 PM
Last Post: manmetdehamer
  Calling URL on event couchninja 5 2,551 06-29-2016, 08:43 PM
Last Post: Niek
  event at randomized time after another event Emiks5 3 1,616 05-07-2016, 09:10 AM
Last Post: Thunder84
  ERROR: rule #21 invalid: variable "label" of device "WindMax" cannot be used in even koos147 2 1,915 04-02-2016, 10:24 PM
Last Post: koos147

Forum Jump:


Browsing: 1 Guest(s)