How to change “ON/OFF” string data into 0/1?
I am monitoring and controlling a smart light bulb from my dashboard(User Interface Dashboard) and every time I open or close the light bulb it sends back a data that shows the light bulb is on or off, what I want to do is to change this "ON/OFF" data string in to binary of 1/0, is there anyway I can get 1 or 0 instead of 'ON/OFF'.
this is some of the API code I have used to control the light bulb
def identifyDevice(self):
identifyDeviceResult = False
print(" {0}Agent for {1} is identifying itself by doing colorloop. Please observe your lights"
.format(self.variables.get('agent_id', None), self.variables.get('model', None)))
try:
devicewasoff = 0
if self.get_variable('status') == "OFF":
devicewasoff = 1
self.setDeviceStatus({"status": "ON"})
elif self.only_white_bulb:
self.setDeviceStatus({"status": "OFF"})
if self.only_white_bulb is False:
self.setDeviceStatus({"effect": "colorloop"})
if self.only_white_bulb:
time_iden = 3
else:
time_iden = 10 # time to do identification
t0 = time.time()
self.seconds = time_iden
while time.time() - t0 <= time_iden:
self.seconds = self.seconds - 1
print("wait: {} sec".format(self.seconds))
time.sleep(1)
self.setDeviceStatus({"effect": "none"})
if devicewasoff == 1:
self.setDeviceStatus({"status": "OFF"})
else:
self.setDeviceStatus({"status": "ON"})
identifyDeviceResult = True
except:
print("ERROR: classAPI_PhilipsHue connection failure! @ identifyDevice")
return identifyDeviceResult
python api
add a comment |
I am monitoring and controlling a smart light bulb from my dashboard(User Interface Dashboard) and every time I open or close the light bulb it sends back a data that shows the light bulb is on or off, what I want to do is to change this "ON/OFF" data string in to binary of 1/0, is there anyway I can get 1 or 0 instead of 'ON/OFF'.
this is some of the API code I have used to control the light bulb
def identifyDevice(self):
identifyDeviceResult = False
print(" {0}Agent for {1} is identifying itself by doing colorloop. Please observe your lights"
.format(self.variables.get('agent_id', None), self.variables.get('model', None)))
try:
devicewasoff = 0
if self.get_variable('status') == "OFF":
devicewasoff = 1
self.setDeviceStatus({"status": "ON"})
elif self.only_white_bulb:
self.setDeviceStatus({"status": "OFF"})
if self.only_white_bulb is False:
self.setDeviceStatus({"effect": "colorloop"})
if self.only_white_bulb:
time_iden = 3
else:
time_iden = 10 # time to do identification
t0 = time.time()
self.seconds = time_iden
while time.time() - t0 <= time_iden:
self.seconds = self.seconds - 1
print("wait: {} sec".format(self.seconds))
time.sleep(1)
self.setDeviceStatus({"effect": "none"})
if devicewasoff == 1:
self.setDeviceStatus({"status": "OFF"})
else:
self.setDeviceStatus({"status": "ON"})
identifyDeviceResult = True
except:
print("ERROR: classAPI_PhilipsHue connection failure! @ identifyDevice")
return identifyDeviceResult
python api
FYI,('on' == 'on')*1
= 1 and('on' == 'off')*1
= 0
– Sotos
Nov 26 '18 at 12:30
add a comment |
I am monitoring and controlling a smart light bulb from my dashboard(User Interface Dashboard) and every time I open or close the light bulb it sends back a data that shows the light bulb is on or off, what I want to do is to change this "ON/OFF" data string in to binary of 1/0, is there anyway I can get 1 or 0 instead of 'ON/OFF'.
this is some of the API code I have used to control the light bulb
def identifyDevice(self):
identifyDeviceResult = False
print(" {0}Agent for {1} is identifying itself by doing colorloop. Please observe your lights"
.format(self.variables.get('agent_id', None), self.variables.get('model', None)))
try:
devicewasoff = 0
if self.get_variable('status') == "OFF":
devicewasoff = 1
self.setDeviceStatus({"status": "ON"})
elif self.only_white_bulb:
self.setDeviceStatus({"status": "OFF"})
if self.only_white_bulb is False:
self.setDeviceStatus({"effect": "colorloop"})
if self.only_white_bulb:
time_iden = 3
else:
time_iden = 10 # time to do identification
t0 = time.time()
self.seconds = time_iden
while time.time() - t0 <= time_iden:
self.seconds = self.seconds - 1
print("wait: {} sec".format(self.seconds))
time.sleep(1)
self.setDeviceStatus({"effect": "none"})
if devicewasoff == 1:
self.setDeviceStatus({"status": "OFF"})
else:
self.setDeviceStatus({"status": "ON"})
identifyDeviceResult = True
except:
print("ERROR: classAPI_PhilipsHue connection failure! @ identifyDevice")
return identifyDeviceResult
python api
I am monitoring and controlling a smart light bulb from my dashboard(User Interface Dashboard) and every time I open or close the light bulb it sends back a data that shows the light bulb is on or off, what I want to do is to change this "ON/OFF" data string in to binary of 1/0, is there anyway I can get 1 or 0 instead of 'ON/OFF'.
this is some of the API code I have used to control the light bulb
def identifyDevice(self):
identifyDeviceResult = False
print(" {0}Agent for {1} is identifying itself by doing colorloop. Please observe your lights"
.format(self.variables.get('agent_id', None), self.variables.get('model', None)))
try:
devicewasoff = 0
if self.get_variable('status') == "OFF":
devicewasoff = 1
self.setDeviceStatus({"status": "ON"})
elif self.only_white_bulb:
self.setDeviceStatus({"status": "OFF"})
if self.only_white_bulb is False:
self.setDeviceStatus({"effect": "colorloop"})
if self.only_white_bulb:
time_iden = 3
else:
time_iden = 10 # time to do identification
t0 = time.time()
self.seconds = time_iden
while time.time() - t0 <= time_iden:
self.seconds = self.seconds - 1
print("wait: {} sec".format(self.seconds))
time.sleep(1)
self.setDeviceStatus({"effect": "none"})
if devicewasoff == 1:
self.setDeviceStatus({"status": "OFF"})
else:
self.setDeviceStatus({"status": "ON"})
identifyDeviceResult = True
except:
print("ERROR: classAPI_PhilipsHue connection failure! @ identifyDevice")
return identifyDeviceResult
python api
python api
asked Nov 26 '18 at 11:08
A.AliA.Ali
84
84
FYI,('on' == 'on')*1
= 1 and('on' == 'off')*1
= 0
– Sotos
Nov 26 '18 at 12:30
add a comment |
FYI,('on' == 'on')*1
= 1 and('on' == 'off')*1
= 0
– Sotos
Nov 26 '18 at 12:30
FYI,
('on' == 'on')*1
= 1 and ('on' == 'off')*1
= 0– Sotos
Nov 26 '18 at 12:30
FYI,
('on' == 'on')*1
= 1 and ('on' == 'off')*1
= 0– Sotos
Nov 26 '18 at 12:30
add a comment |
1 Answer
1
active
oldest
votes
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True
and 1
are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int
to serialise, call int
with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) callint
. I'll update the answer.
– Holloway
Nov 26 '18 at 12:21
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479834%2fhow-to-change-on-off-string-data-into-0-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True
and 1
are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int
to serialise, call int
with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) callint
. I'll update the answer.
– Holloway
Nov 26 '18 at 12:21
add a comment |
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True
and 1
are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int
to serialise, call int
with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) callint
. I'll update the answer.
– Holloway
Nov 26 '18 at 12:21
add a comment |
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True
and 1
are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int
to serialise, call int
with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0
If you can guarantee that the returned value is always one of 'ON' or 'OFF', you can use the fact that True
and 1
are interchangeable
result = state == 'ON'
If you need some error handling, something like this might work.
if state in {'ON', 'OFF'}:
result = state == 'ON'
else:
# handle error state
If you need an int
to serialise, call int
with the result
>>> state_on = 'ON'
>>> state_off = 'OFF'
>>> int(state_on == 'ON')
1
>>> int(state_off == 'ON')
0
edited Nov 26 '18 at 12:23
answered Nov 26 '18 at 11:11
HollowayHolloway
2,94311828
2,94311828
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) callint
. I'll update the answer.
– Holloway
Nov 26 '18 at 12:21
add a comment |
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) callint
. I'll update the answer.
– Holloway
Nov 26 '18 at 12:21
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
it didn't work, Am still getting on/off message! is there any other way possible to get the binary data of 1 and 0
– A.Ali
Nov 26 '18 at 11:26
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
What do you mean binary data of 1 and 0? Is that just the int? Where are you getting the on/off message?
– Holloway
Nov 26 '18 at 11:46
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
Yeah I need the int of 1 and 0 instead of on and off, I am sending the data of a smart bulb to a local gateway with MQTT protocol, which published on and off message.
– A.Ali
Nov 26 '18 at 11:50
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) call
int
. I'll update the answer.– Holloway
Nov 26 '18 at 12:21
If you need to ensure it's an actual integer instead of just being usable as one (for sending out of Python) call
int
. I'll update the answer.– Holloway
Nov 26 '18 at 12:21
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53479834%2fhow-to-change-on-off-string-data-into-0-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
FYI,
('on' == 'on')*1
= 1 and('on' == 'off')*1
= 0– Sotos
Nov 26 '18 at 12:30